Find a row in dataGridView based on column and value

前端 未结 7 1459
轻奢々
轻奢々 2020-12-07 14:54

I have a dataGridView that has 3 columns: SystemId, FirstName, LastName that is bound using database information. I would like to highlight a certain row, which I would do

7条回答
  •  无人及你
    2020-12-07 15:31

    Or you can use like this. This may be faster.

    int iFindNo = 14;
    int j = dataGridView1.Rows.Count-1;
    int iRowIndex = -1;
    for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count/2) +1; i++)
    {
        if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == iFindNo)
        {
            iRowIndex = i;
            break;
        }
        if (Convert.ToInt32(dataGridView1.Rows[j].Cells[0].Value) == iFindNo)
        {
            iRowIndex = j;
            break;
        }
        j--;
    }
    if (iRowIndex != -1)
        MessageBox.Show("Index is " + iRowIndex.ToString());
    else
        MessageBox.Show("Index not found." );
    

提交回复
热议问题