Accessing Selected DataRow from DataGrid

G

Guest

I'm having difficulty with identifying the selected DataRow in my DataGrid.
Here is the situation:

I have a DataSet with several tables and relationships between the tables.
This situation concerns a parent child table pair. I created a DataView for
my parent table. The child table is related with a ParentToChild
relationship. I defined two DataGridTableStyles, one each for the parent and
child tables and added them to the DataGrid.TableStyles.

The user selects a Parent row from the DataGrid DataView and then I use the
DataGrid.NavigateTo(myHitTest.Row, "ParentToChild") method to show the
related Child table rows. They are displayed as defined by the associated
DataGridTableStyles. Everything works find to this point.

Now, however, the user selects a row from the DataGrid and I need to know
the child table row that has been selected. I’ve tried several approaches but
with no success. Here is my latest attempt:

int intRow = 0;
foreach( DataRowView drv in myDataView )
if( myDataGrid.IsSelected(intRow++) )
OpenEntry( (int)drv.Row["entry_id"] );

The problem I have is that myDataView still returns the Parent table rows
and myDataGrid still refers to the Parent table as well (i.e. the selected
row is the one selected in the Parent table not the Child table rows
currently displayed).

How do I get at the data behind the displayed view?
 
C

ClayB [Syncfusion]

Try using the CurrencyManager.Current to retrieve the DataRowView object.

CurrencyManager cm = (CurrencyManager)
myDataGrid.BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
DataRowView drv = cm.Current;


=================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
C

ClayB [Syncfusion]

Missed a cast.

DataRowView drv = cm.Current as DataRowView;


=========================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools

ClayB said:
Try using the CurrencyManager.Current to retrieve the DataRowView object.

CurrencyManager cm = (CurrencyManager)
myDataGrid.BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
DataRowView drv = cm.Current;


=================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools


Fleckman said:
I'm having difficulty with identifying the selected DataRow in my DataGrid.
Here is the situation:

I have a DataSet with several tables and relationships between the tables.
This situation concerns a parent child table pair. I created a DataView for
my parent table. The child table is related with a ParentToChild
relationship. I defined two DataGridTableStyles, one each for the parent and
child tables and added them to the DataGrid.TableStyles.

The user selects a Parent row from the DataGrid DataView and then I use the
DataGrid.NavigateTo(myHitTest.Row, "ParentToChild") method to show the
related Child table rows. They are displayed as defined by the associated
DataGridTableStyles. Everything works find to this point.

Now, however, the user selects a row from the DataGrid and I need to know
the child table row that has been selected. I've tried several
approaches
but
with no success. Here is my latest attempt:

int intRow = 0;
foreach( DataRowView drv in myDataView )
if( myDataGrid.IsSelected(intRow++) )
OpenEntry( (int)drv.Row["entry_id"] );

The problem I have is that myDataView still returns the Parent table rows
and myDataGrid still refers to the Parent table as well (i.e. the selected
row is the one selected in the Parent table not the Child table rows
currently displayed).

How do I get at the data behind the displayed view?
 
G

Guest

Thanks ClayB. That answers my fundamental question but leaves me with one
remaining. That is, how can I identify the rows when there are multiple
selected.

ClayB said:
Missed a cast.

DataRowView drv = cm.Current as DataRowView;


=========================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools

ClayB said:
Try using the CurrencyManager.Current to retrieve the DataRowView object.

CurrencyManager cm = (CurrencyManager)
myDataGrid.BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
DataRowView drv = cm.Current;


=================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools


Fleckman said:
I'm having difficulty with identifying the selected DataRow in my DataGrid.
Here is the situation:

I have a DataSet with several tables and relationships between the tables.
This situation concerns a parent child table pair. I created a DataView for
my parent table. The child table is related with a ParentToChild
relationship. I defined two DataGridTableStyles, one each for the parent and
child tables and added them to the DataGrid.TableStyles.

The user selects a Parent row from the DataGrid DataView and then I use the
DataGrid.NavigateTo(myHitTest.Row, "ParentToChild") method to show the
related Child table rows. They are displayed as defined by the associated
DataGridTableStyles. Everything works find to this point.

Now, however, the user selects a row from the DataGrid and I need to know
the child table row that has been selected. I've tried several
approaches
but
with no success. Here is my latest attempt:

int intRow = 0;
foreach( DataRowView drv in myDataView )
if( myDataGrid.IsSelected(intRow++) )
OpenEntry( (int)drv.Row["entry_id"] );

The problem I have is that myDataView still returns the Parent table rows
and myDataGrid still refers to the Parent table as well (i.e. the selected
row is the one selected in the Parent table not the Child table rows
currently displayed).

How do I get at the data behind the displayed view?
 
A

Andy Becker

Fleckman said:
Thanks ClayB. That answers my fundamental question but leaves me with one
remaining. That is, how can I identify the rows when there are multiple
selected.

I think you get to roll your own with DataGrid.IsSelected(row).

Best Regards,

Andy
 

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