Select Row in the DataGridView

Hello...

I know just the key value and 
want to select the row in the DataGridView using that value.

I tried to use the TypedDataSet and DataGridView.

But, I don't know the row index in the DataGridView.


The job what I want is as below...

int liIndex = xxxxxxxxxxxxxxxxxxxx;
dgWindow.FirstDisplayedScrollingRowIndex = liIndex;
dgWindow.Rows[liIndex].Selected = true;
dgWindow.CurrentCell = dgMenu[0, liIndex];

And, "liIndex" is what I don't know to solve.
How can I get the rowindex from the specified value

Please, answer me~~

Thank you~~


Answer this question

Select Row in the DataGridView

  • Burazer

    Thank you for your answer.

    That is what I want, but I want to know whether the simeple method exist.

    Thank you anyway... Smile

  • winstonSmith

    Hi,

    I understood your post in the way that you want to look for a value in the datagridview and then get the row index where the value is in. I hope I understood you right. Here is some code:


    int lindex = -1;

    string myvalue = "red";

    foreach (DataGridViewRow dr in dataGridView1.Rows)

    {

    MessageBox.Show(dr.Cells[1].Value.ToString());

    if (dr.Cells[1].Value.ToString() == myvalue)

    {

    lindex = dataGridView1.Rows.IndexOf(dr);

    break;

    }

    }


     



  • HiddenDragon

    Thank you very much. Big Smile
  • cguanchez

    Hi,

    What is the DataSource of your grid If its a datatable then you could use the DataTable.Select method. It acts like a WHERE method in your sql statement. And it returns an array of DataRows...

     

     

    cheers,

    Paul June A. Domag



  • Dwayne Need

    Hi,

    is for you a simple method to look in the datagridview with a sql like language

    I think such a thing will be implemented with C# 3.0 and Linq.

    http://msdn.microsoft.com/vcsharp/future/linqsamples/projection/default.aspx#simple1

    but I did not find a fitting sample to send you. I will ask if it is possible to search in a DataGridview in the Link Forum.

    http://forums.microsoft.com/msdn/ShowPost.aspx PostID=93601#93601

  • Select Row in the DataGridView