views:

233

answers:

3

Hi,

Can anyone help me please?

I have to find the row number from 1 selected array which I stored in one separate array and randomly I'm trying to get the row number from datagridview

http://serv8.enterupload.com/i/00246/aewglahmvpr5.jpg

In other words: if I know a column value for a given row in a datagridview (e.g. for this row, FirstName == 'Bud'), how can I get the row ID?

A: 

There's probably some easier way where you filter it in some way but at the moment I can only think of looping through it.

int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
    if(row.Cells(1).Value.ToString().Equals("mystr"))
    {
        rowIndex = row.Index;
        break;
    }
}
// rowIndex is now either the correct index or -1 if not found
ho1
A: 

You can use LINQ query:

        int index = -1;

        DataGridViewRow row = dgv.Rows
            .Cast<DataGridViewRow>()
            .Where(r => r.Cells[columnId].Value.ToString().Equals("Some string"))
            .First();

        index = row.Index;
Pavel Belousov
i dont know the cells id i just stored all the cell in one saprate array, randomly i chosen from that array and i want? in which row its belongs
Yes, it's right. But you know column, and this query will select first row which have "Some string" in your column.
Pavel Belousov
A: 

From: http://www.vbforums.com/showthread.php?t=610134

Call the Find method of the BindingSource and it will return the index of the matching row if there is one. If there is, you index the BindingSource to get that row and update the appropriate fields. If there isn't, you call the AddNew method of the BindingSource to create a new row and set the appropriate fields.

openshac