select a row with code

kcwallace

Well-known member
Joined
May 27, 2004
Messages
172
Location
Irvine, CA
Can someone guide me to an example for setting the row of a DataGridView programmatically? For Examle:

DataGridView1.row=12

I tried:

DataGridView1.Rows(yyy).Selected = True

but that only highlights the row.
 
Code:
DataGridView1.Rows(12).Selected = True
should do it
Make sure to Set SelectionMode = FullRowSelect




kcwallace said:
Can someone guide me to an example for setting the row of a DataGridView programmatically? For Examle:

DataGridView1.row=12

I tried:

DataGridView1.Rows(yyy).Selected = True

but that only highlights the row.
 
Ah ok then you want to EDIT that cell , i think.

.BeginEdit() should do it .. just make sure you have already selected it

its funny you ask this, i just figured this out a few days ago. Also, its important to make sure you have only 1 cell selected. Oddly, I had to use .MultiSelect = False before I used .BeginEdit().
.ClearSelection() didnt seem to clear the selections alone, oddly.

so i did
Code:
        dgrid1.SelectionMode = DataGridViewSelectionMode.CellSelect
        dgrid1.MultiSelect = False
        dgrid1.ClearSelection()
        dgrid1.Item(column, row).Selected = True
        dgrid1.Item(colum, row).ReadOnly = False
        dgrid1.BeginEdit(False)
some of that is probably unnecessary



kcwallace said:
ALl that does is highlights the cell, it does not change the focus to that row
 
No, but I figured it out myself.

You must:

On form load
Code:
Dim bs As New BindingSource
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet

da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds)
        bs.DataSource = ds
        bs.DataMember = ds.Tables(0).TableName

Dim dv As DataGridView = DataGridView1

        dv.DataSource = bs

trigger the change of selected records

Code:
Dim yyy = 22 or any other desired row
DataGridView1.Rows(yyy).Selected = True
DataGridView1.FirstDisplayedScrollingRowIndex = yyy
bs.Position = yyy
 
Back
Top