473,498 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView row selection

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
Jul 21 '06 #1
2 14495
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(primary Key));

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" <le**@comhem.sewrote in message
news:uK**************@TK2MSFTNGP02.phx.gbl...
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

Jul 21 '06 #2
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

"Dave Sexton" <dave@jwa[remove.this]online.comskrev i meddelandet
news:%2****************@TK2MSFTNGP05.phx.gbl...
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(primary Key));

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" <le**@comhem.sewrote in message
news:uK**************@TK2MSFTNGP02.phx.gbl...
>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


Jul 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1356
by: Bob | last post by:
1- Inability to easily reset datagridview combobox selection to null Solution - add a contextmenustrip to the column in which you have the combobox on the datagridview. show to user, something like...
0
4970
by: Michel Lapointe | last post by:
Hello, I would like to know if there is an event for the Datagridview control that is trigger before the selection change and is cancelable. I'm currently using the SelectionChanged event...
2
13365
by: tony lock | last post by:
Has anybody found a way to disable a column in DataGridView i.e prevent it being selected. With DataGrid it was fairly easy to check on the column being entered and move the selection to the next...
4
4745
by: steve | last post by:
Hi All I have a datagridview bound to a datatable Due to the use of touch screens I need to be able to scroll the grid in code in response to a button click i.e each button touch (click) will...
6
50383
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to...
0
1498
by: Paul Cheetham | last post by:
Hi, I have a DataGridView control, where I am setting its Datasource property to a datatable. The DataGridView has it's MultiSelect property set to false I also have a handler for the...
0
1327
by: DBC User | last post by:
I have a datagridview and there are 4 coulmns, the user could do sort on any of the columns. After doing sorting. The user could do refresh the datagridview and get another set of data. But when I...
4
6024
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am trying to populate a DataGridView Combobox column with a different data binding source (dataset) depending upon a selection made in a previous column. I can handle the previous column but how...
2
5918
by: colin | last post by:
Hi, I need to tell how many rows are visible on the screen, in my DataGridView control so that I can ensure my newly selected row is visible without doing any unecessry scrolling, I thought...
6
4395
by: hzgt9b | last post by:
Using VS2005, VB.NET, I have a windows app with a DataGridView (lets call it DGV). At some point in the life of my app I want to clear the selection of the currently selected row...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.