直接上代码:
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void FindDataForm_FindEvent(object sender, FindEventArgs args)
{
bool ifbreak = false;
if (FindedRowIndex == 0 && FindedColumnIndex == 0)
{
if (FindedRowIndex != dbView.CurrentCell.RowIndex)
{
FindedRowIndex = dbView.CurrentCell.RowIndex;
}
if (FindedColumnIndex != dbView.CurrentCell.ColumnIndex)
{
FindedColumnIndex = dbView.CurrentCell.ColumnIndex;
}
}
#region 判断当前值是不是匹配结果,是直接查找下一个而不取当前值
var currentVal = dbView.CurrentCell.Value.ToString();
var RegexStr = args.val;
MatchCollection r = null;
if (args.isFullCharacter)
{
RegexStr = "^" + args.val + "$";
}
if (!args.isCase)
{
r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
}
else
{
r = Regex.Matches(currentVal, RegexStr);
}
#endregion
if (args.direction == 0)
{
if (r.Count > 0)
{
if (dbView.CurrentCell.ColumnIndex == 0)
{
FindedRowIndex = dbView.CurrentCell.RowIndex - 1;
if (FindedRowIndex == -1)
{
FindedColumnIndex = -1;
}
else {
FindedColumnIndex = dbView.Columns.Count - 1;
}
}
else {
FindedColumnIndex = dbView.CurrentCell.ColumnIndex - 1;
}
}
if (FindedRowIndex == dbView.Rows.Count)
{
FindedRowIndex--;
}
if (FindedColumnIndex == dbView.Columns.Count)
{
FindedColumnIndex--;
}
while (FindedRowIndex > -1)
{
while (FindedColumnIndex > -1)
{
currentVal = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex].Value.ToString();
RegexStr = args.val;
r = null;
if (args.isFullCharacter)
{
RegexStr = "^" + args.val + "$";
}
if (!args.isCase)
{
r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
}
else
{
r = Regex.Matches(currentVal, RegexStr);
}
if (r.Count > 0)
{
dbView.CurrentCell = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex];
ifbreak = true;
}
//当遍历到一行的第一列时将行索引减一,列索引回到最大值(逐行扫描)
if (FindedColumnIndex == 0)
{
FindedRowIndex--;
//第一行的第一列,列索引不归零
if (FindedRowIndex > -1)
{
FindedColumnIndex = dbView.Columns.Count;
}
}
FindedColumnIndex--;
if (ifbreak)
{
break;
}
}
if (ifbreak)
{
break;
}
}
}
else
{
if (r.Count > 0)
{
if (dbView.CurrentCell.ColumnIndex == dbView.Columns.Count - 1)
{
FindedRowIndex = dbView.CurrentCell.RowIndex + 1;
if (FindedRowIndex == dbView.Rows.Count)
{
FindedColumnIndex = dbView.CurrentCell.ColumnIndex + 1;
}
else {
FindedColumnIndex = 0;
}
}
else
{
FindedColumnIndex = dbView.CurrentCell.ColumnIndex + 1;
}
}
if (FindedRowIndex == -1) {
FindedRowIndex++;
}
if (FindedColumnIndex == -1)
{
FindedColumnIndex++;
}
while (FindedRowIndex < dbView.Rows.Count)
{
if (FindedRowIndex == dbView.Rows.Count && FindedColumnIndex == dbView.Columns.Count)
{
MessageBox.Show("没有更多记录可供查询。");
break;
}
while (FindedColumnIndex < dbView.Columns.Count)
{
currentVal = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex].Value.ToString();
RegexStr = args.val;
r = null;
if (args.isFullCharacter)
{
RegexStr = "^" + args.val + "$";
}
if (!args.isCase)
{
r = Regex.Matches(currentVal, RegexStr, RegexOptions.IgnoreCase);
}
else
{
r = Regex.Matches(currentVal, RegexStr);
}
if (r.Count > 0)
{
dbView.CurrentCell = dbView.Rows[FindedRowIndex].Cells[FindedColumnIndex];
ifbreak = true;
}
FindedColumnIndex++;
//当遍历到一行的最后一列时将行索引加一,列索引归零(逐行扫描)
if (FindedColumnIndex == dbView.Columns.Count)
{
FindedRowIndex++;
//最后一行的最后一列,列索引不归零
if (FindedRowIndex < dbView.Rows.Count)
{
FindedColumnIndex = 0;
}
}
if (ifbreak)
{
break;
}
}
if (ifbreak)
{
break;
}
}
}
if (!ifbreak)
{
MessageBox.Show("未能找到更多结果。");
}
}