Current Selected Row in a DataGrid

G

Guest

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
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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
 
G

Guest

** 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 said:
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 said:
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 said:
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
 
C

Cor Ligthert [MVP]

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
 

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