DataGridView row selection

L

Lennart

Hi

I have a DataGridView on a Form filled with data from a DataTable. When I
Load this form I want to select a row in the DataGridView depending on a
selected Primary Key index;

How do I do this?

/Lennart
 
D

Dave Sexton

Hi Lennart,

I'm not exactly sure what you are asking, but here is some code that will
select the row of the DataGridView that is bound to a DataRow with a
specific primary key value:

int primaryKey = 5;

// assuming dataGridView1 is bound to dataTable
int rowIndex = dataTable.Rows.IndexOf(dataTable.Rows.Find(primaryKey));

dataGridView1.Rows[rowIndex].Selected = true;

You can replace "dataTable.Rows" with "dataView" in the above code if
dataView is a DataView to which the DataGridView is bound instead of
dataTable.

- Dave Sexton
 
L

Lennart

Hi Dave

You put me on the right track, but the correct way turned out to be this.

dataGridView1.FirstDisplayedScrollingRowIndex =
myTable.Rows.IndexOf(myTable.Rows.Find(myIndex));

dataGridView1.CurrentCell = dataGridView1.FirstDisplayedCell;

/Lennart

Hi Lennart,

I'm not exactly sure what you are asking, but here is some code that will
select the row of the DataGridView that is bound to a DataRow with a
specific primary key value:

int primaryKey = 5;

// assuming dataGridView1 is bound to dataTable
int rowIndex = dataTable.Rows.IndexOf(dataTable.Rows.Find(primaryKey));

dataGridView1.Rows[rowIndex].Selected = true;

You can replace "dataTable.Rows" with "dataView" in the above code if
dataView is a DataView to which the DataGridView is bound instead of
dataTable.

- Dave Sexton

Lennart said:
Hi

I have a DataGridView on a Form filled with data from a DataTable. When
I Load this form I want to select a row in the DataGridView depending on
a selected Primary Key index;

How do I do this?

/Lennart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top