finding clicked row in datagrid?

V

VMI

Hi,
I have a Windows datagrid that's attached to a filled datatable. The grid
also has a tablestyle. Since the data from the grid comes from a
datatatable, how can I retrieve the data from a specific table row once a
user clicks on a grid row? I already have the code that lets me know what
grid row the user clicked on, but, for example, if I want to retrieve the
value of the 5th column in the grid, I'm currently using myGrid[rowNum,
4].ToString(). But If I decide to change the position of "Name" to column 8,
I'll have to change this code (eg. myGrid[rowNum, 7].ToString() ). Is there
any other alternative?

Thanks.
 
R

Rakesh Rajan

Hi VMI,

Your question as I understood is this:
you have a datagrid, bound to a datatable. The columns, or the sorting etc.
might change in the datagrid.
Regardless of the column position, or the current sort, you want to obtain
the datarow which the user has currently selected in the datagrid. Is this
right?

If yes, then you could use the Binding Manager to find out the current
object/row that the bound datagrid is now referring to. For example, to get
the current row, use:
((CurrencyManager)BindingContext[datatablename]).Current.
Cast this into a datarow (or your typed one), and then use it.

This is a good article which explains the data binding procedure in windows
forms:
http://msdn.microsoft.com/library/en-us/dndotnet/html/databindingadonet.asp

HTH,
Rakesh Rajan
 
V

VMI

Thanks. It worked.
Can I use the Binding Manager to retrieve all the rows that are highlighted
(instead of just one)?
In my code, this is how I use it when the row is already highlighted:

DataRowView rowViewAudit = (DataRowView)
((CurrencyManager)BindingContext[_table_auditAddress]).Current;

But this only works when there's only one line chosen. I tried declaring
rowViewAudit as DataRowView[] and use foreach, but it doesn't work.

Thanks.


Rakesh Rajan said:
Hi VMI,

Your question as I understood is this:
you have a datagrid, bound to a datatable. The columns, or the sorting etc.
might change in the datagrid.
Regardless of the column position, or the current sort, you want to obtain
the datarow which the user has currently selected in the datagrid. Is this
right?

If yes, then you could use the Binding Manager to find out the current
object/row that the bound datagrid is now referring to. For example, to get
the current row, use:
((CurrencyManager)BindingContext[datatablename]).Current.
Cast this into a datarow (or your typed one), and then use it.

This is a good article which explains the data binding procedure in windows
http://msdn.microsoft.com/library/en-us/dndotnet/html/databindingadonet.asp

HTH,
Rakesh Rajan

VMI said:
Hi,
I have a Windows datagrid that's attached to a filled datatable. The grid
also has a tablestyle. Since the data from the grid comes from a
datatatable, how can I retrieve the data from a specific table row once a
user clicks on a grid row? I already have the code that lets me know what
grid row the user clicked on, but, for example, if I want to retrieve the
value of the 5th column in the grid, I'm currently using myGrid[rowNum,
4].ToString(). But If I decide to change the position of "Name" to
column
8,
I'll have to change this code (eg. myGrid[rowNum, 7].ToString() ). Is there
any other alternative?

Thanks.
 

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