472,127 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Current Selected Row in a DataGrid

Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve
Aug 11 '05 #1
4 15023
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Tables[0].DefaultView[dgJobs.CurrentRowIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #2

That doesnt seem to work, it points to the same record as just doing

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

Do i need to set the default view somehow?
"Ben Reese" wrote:
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Tables[0].DefaultView[dgJobs.CurrentRowIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #3
** SOLVED **

I did this:
CurrencyManager xCM =
(CurrencyManager)dgJobs.BindingContext[dgJobs.DataSource, dgJobs.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
DataRow xRow = xDRV.Row;

xRow is then a pointer to the current selected row in my datagrid.

Cheers

"Steve" wrote:

That doesnt seem to work, it points to the same record as just doing

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

Do i need to set the default view somehow?
"Ben Reese" wrote:
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Tables[0].DefaultView[dgJobs.CurrentRowIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Tables[0].Rows[dgJobs.CurrentRowIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #4
Steve,

Assuming that your datagrid is a windowforms datagrid, than the
currencymanager position gives you the position of the current row number in
the datasource.

((CurrencyManager)BindingContext[dataGrid1.DataSource]).Position;

I hope this helps,

Cor
Aug 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by amber | last post: by
2 posts views Thread by mrstrong | last post: by
3 posts views Thread by Christopher Weaver | last post: by
4 posts views Thread by Steve | last post: by

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.