public static bool SearchDGV(DataGridView DGV, string strTxt, bool UpSearch = true, bool Show = false)
{
int row = DGV.Rows.Count;//得到总行数
int cell = DGV.Rows[].Cells.Count;//得到总列数
int curr = DGV.CurrentRow.Index;
Regex r = new Regex(strTxt); // 定义一个Regex对象实例
for (int i = curr + ; i < row; i++)//得到总行数并在之内循环
{
for (int j = ; j < cell; j++)//得到总列数并在之内循环
{
Match m = r.Match(DGV.Rows[i].Cells[j].Value.ToString()); // 在字符串中模糊匹配
if (m.Success)
{ //对比TexBox中的值是否与dataGridView中的值相同(上面这句)
DGV.CurrentCell = DGV[j, i];//定位到相同的单元格
if (Show)
{
if (MessageBox.Show("是否需要继续查找?", "", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
//如果选择了取消就会返回,如果选择了确定,就会继续查找匹配的.
return true;//返回
}
}
else
{
return true;
}
}
}
}
if (UpSearch)
{
for (int i = ; i < row; i++)//得到总行数并在之内循环
{
for (int j = ; j <= curr; j++)//得到总列数并在之内循环
{
Match m = r.Match(DGV.Rows[i].Cells[j].Value.ToString()); // 在字符串中模糊匹配
if (m.Success)
{ //对比TexBox中的值是否与dataGridView中的值相同(上面这句)
DGV.CurrentCell = DGV[j, i];//定位到相同的单元格
if (Show)
{
if (MessageBox.Show("是否需要继续查找?", "", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
//如果选择了取消就会返回,如果选择了确定,就会继续查找匹配的.
return true;//返回
}
}
else
{
return true;
}
}
}
}
}
return false;
}