views:

142

answers:

2

I know this will be easy, but I can't seem to find it anywhere. How do you SET the current row in a gridview? I find tons of ways to get data from it, but I what to set a current row or cell programatically. I'm using VB 2008 express. I also find lots of promising properties like Selected... but these are all read only and i can't set them.

+2  A: 

You can use the SelectedIndex property to set the current row.

RBarryYoung
For more info on the SelectedIndex Property go to: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindex.aspx
Americus
A: 

This might work for you. Let's say you have a need to select all the rows where we have more than 100 items in stock:

Private Sub btnSelectRow_Click(object sender, EventArgs e) Handles btnSelectRow.Click
    For Each r1 as DataGridViewRow in dataGridView1.Rows
        If r1.IsNewRow Then
            Exit For
        End If
        If Convert.ToInt32(r1.Cells(5).Value) > 100 Then
            r1.Selected = True
        End If
    Next        
End Sub

Here's the same thing in C#:

private void btnSelectRow_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow r1 in this.dataGridView1.Rows)
    {
        if (r1.IsNewRow) break;
        if ((int)r1.Cells[5].Value > 100)
        {
            r1.Selected = true;
        }
    }
}

Of course you could use any criteria for selecting a row, but that gives you an idea. Hope that helps.

Rap