1、 C#在dataGridView中遍历,寻找相同的数据并定位
- 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 (txtFind.Text == dataGridView1.Rows.Cells[j].Value.ToString())
- {
- this.dataGridView1.CurrentCell = this.dataGridView1[j, i];
- return;
- }
-
- }
- }
2 、可以实现模糊查询了
- int row = dataGridView1.Rows.Count;
- int cell=dataGridView1.Rows[1].Cells.Count;
- string strTxt = txtFind.Text;
- Regex r = new Regex(strTxt);
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < cell; j++)
- {
- Match m = r.Match(dataGridView1.Rows.Cells[j].Value.ToString());
- if (m.Success)
- {
- dataGridView1.CurrentCell = dataGridView1[j,i];
- return;
- }
-
- }
- }
3、可以决定是否还要继续查找了
- private void button3_Click(object sender, EventArgs e)
- {
- int row = dataGridView1.Rows.Count;
- int cell=dataGridView1.Rows[1].Cells.Count;
- string strTxt = txtFind.Text;
- Regex r = new Regex(strTxt);
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < cell; j++)
- {
- Match m = r.Match(dataGridView1.Rows.Cells[j].Value.ToString());
- if (m.Success)
- {
- dataGridView1.CurrentCell = dataGridView1[j,i];
- if (MessageBox.Show("是否需要继续查找?", "", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
- {
-
- return;
- }
- }
- }
- }
- }
|