文档库 最新最全的文档下载
当前位置:文档库 › C# dataGridView1 单元格数据查找(精确+模糊)定位

C# dataGridView1 单元格数据查找(精确+模糊)定位

//查找并定位dataGridView1 单元格

public static int RowCount = 0;
///记录已查找过的行数
public static int SetGetRow
{
set
{
if (RowCount != value) { RowCount = value; }
}
get { return RowCount; }
}
///
/// 查找相应内容并定位表格(精确+模糊)
/// /// 要查找的字符串内容
/// 要查找的表格名称

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//int i = 0;//计数器
//foreach (DataGridViewRow dr in this.dataGridView1.Rows)
//{
// if (e.KeyCode == Keys.Enter)
// {
// if (dr.Cells["产品编码"].Value.ToString() == this.textBox1.Text)
// {
// this.dataGridView1.FirstDisplayedScrollingRowIndex = i;//将当前找到的行设置到第一行

// this.dataGridView1.CurrentCell = dr.Cells[0];//去掉其他行的选择状态并将当前行的第一个单元格置为选择状态
// dr.Selected = true;//选择当前整行
// return;
// }
// i++;
// }
//}
if (this.tabControl1.SelectedIndex == 0)
{
//int row = dataGridView1.Rows.Count;//得到总行数
//int cell = dataGridView1.Rows[1].Cells.Count;//得到总列数
//for (int i = 0; i < row; i++)//得到总行数并在之内循环
//{
// for (int j = 0; j < cell; j++)//得到总列数并在之内循环
// {
// if (e.KeyCode == Keys.Enter)
// {
// if (this.textBox1.Text == dataGridView1.Rows[i].Cells[j].Value.ToString())
// { //对比TexBox中的值是否与dataGridView中的值相同(上面这句)
// this.dataGridView1.CurrentCell = this.dataGridView1[j, i];//定位到相同的单元格
// //this.dataGridView1.CurrentCell = this.dataGridView1.Rows[i].Cells[j];//定位到相同的单元格
// this.dataGridView1.Rows[i].Selected = true; //选择当前整行
// return;//返回
// }
// else
// {
// MessageBox.Show("未找到匹配的数据", "提示",
// MessageBoxButtons.OK, MessageBoxIcon.Warning);
// return;
// }


// }

// }

//}
if (e.KeyCode == Keys.Enter)
{
int row = dataGridView1.Rows.Count;//得到总行数
int cell = dataGridView1.Rows[1].Cells.Count;//得到总列数
int _length = this.textBox1.Text.Trim().Length;
for (int i = SetGetRow; i < row; i++)//得到总行数并在之内循环
{
for (int j = 0; j < cell; j++)//得到总列数并在之内循环
{ //精确查找定位
if (this.textBox1.Text.Trim() == dataGridView1.Rows[i].Cells[j].Value.ToString().Trim())
{ //对比TexBox中的值是否与dataGridView中的值相同(上面这句)
dataGridView1.CurrentCell = dataGridView1[j, i];//定位到相同的单元格
dataGridView1.Rows[i].Selected = true;//定位到行
SetGetRow = i + 1; return;//返回
} //模糊查找定位(连续长度相同才认为是相似) /*************模糊查找定位算法 * 从1到对应的表格内容长度查找 * 先找到第一个字符与要查找的内容对应的第一个字符相同的然后查找后面的相同长度的内容是否相同,相同则定位到此行 */
for (int k = 0; k < dataGridView1.Rows[i].Cells[j].Value.ToString().Trim().Length; k++)
{
if (_length <= dataGridView1.Rows[i].Cells[j].Value.ToString().Trim().Length - k)//判断要查找内容的长度是否小于对比的内容的长度
{
if (this.textBox1.Text.Trim().Substring(0, 1) == dataGridView1.Rows[i].Cells[j].Value.ToString().Trim().Substring(k, 1))//判断第一个字符是否与要对比的内容的第一个字符相同
{
if (this.textBox1.Text.Trim() == dataGridView1.Rows[i].Cells[j].Value.ToString().Trim().Substring(k, _length))//判断是查找内容与对比内容否相等
{
dataGridView1.CurrentCell = dataGridView1[j, i];//定位到相同的单元格
dataGridView1.Rows[i].Selected = true;//定位到行
SetGetRow = i + 1; return;//返回
}
}
}
}
}
}
SetGetRow = 0;
MessageBox.Show("没有再次找到相关记录,或没有与之相似的记录!", "快速定位", MessageBoxButtons.OK, https://www.wendangku.net/doc/3716567926.html,rmation);

}

}
}

相关文档
相关文档 最新文档