Returning Correct DataTable Row from DataGrid

R

Randy

I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_MouseDown returns a Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy
 
M

Marco Martin

Randy,

Once you have your datagrid row, you can select against your source table.
But you will need to have a unique-valued column.
for example, if you have an "ID" column;

DataTable tblSource = datagrid.source as DataTable;
DataRow[] rowSelected = tblSource.Select("ID" + dataGrid[e.Row,
nIDColumn].ToString());//Where nIDColumn is the column that holds unique
values

When using a Datable.Select(), it is possible that more than one row meets
the Select criteria. But if your column has unique values, then the result
you want is always at index 0. So in this case rowSelected[0] would be your
only result.

hope this helps,

regards,

Marco
 
D

Dmitriy Lapshin [C# / .NET MVP]

CurrencyManager cm = (CurrencyManager)this.BindingContext[
dataGrid1.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
DataRowView rowView = view[cm.Position];
DataRow row = rowView.Row;
 
R

Randy

Marco,

Many thanks. That works!

Randy

Marco Martin said:
Randy,

Once you have your datagrid row, you can select against your source table.
But you will need to have a unique-valued column.
for example, if you have an "ID" column;

DataTable tblSource = datagrid.source as DataTable;
DataRow[] rowSelected = tblSource.Select("ID" + dataGrid[e.Row,
nIDColumn].ToString());//Where nIDColumn is the column that holds unique
values

When using a Datable.Select(), it is possible that more than one row meets
the Select criteria. But if your column has unique values, then the result
you want is always at index 0. So in this case rowSelected[0] would be your
only result.

hope this helps,

regards,

Marco

Randy said:
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_MouseDown returns a Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy
 
R

Randy

Dmitriy,

Many thanks. That works too!

Randy

Dmitriy Lapshin said:
CurrencyManager cm = (CurrencyManager)this.BindingContext[
dataGrid1.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
DataRowView rowView = view[cm.Position];
DataRow row = rowView.Row;

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Randy said:
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_MouseDown returns a Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy
 

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