Well, you are looking in your data table, not the view, which contains
information on the sort. You are better off using the indexer on the view
with the row index, using the DataRowView (from which you can get the
DataRow, or access the fields in the row through the DataRowView).
Or, you can get the binding context for the grid through the
BindingContext property, and pass the DataView used as a data source on the
grid (and the DataMember, if you are using that) to the BindingContext to
get a BindingManagerBase. From there, you can get the current item, which
should give you the DataRowView, like so:
// Get the BindingManagerBase.
BindingManagerBase bindingManagerBase =
dataGridView1.BindingContext.Item[dataGridView1.DataSource,
dataGridView1.DataMember];
// Get the current item, which should be a data row view.
DataRowView dataRowView = (DataRowView) bindingManagerBase.Current;
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"clogg02" <cr*****@yahoo.co.ukwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
Hi,
I have bound a DataView to a DataGridView using
this.dataGridView1.DataSource = new
DataView(this.dataTable1);
I allow for sorting of the datagridview using clicks on the headers.
Later on I need to determine the content of any selected cell or row
in that datagridview once a button has been pressed.
My event handler:
private void button1_Click(object sender, EventArgs e)
{
DataView dv1 = ((DataView)this.dataGridView1.DataSource);
if (dv1 != null)
{
DataTable dt = dv1.Table;
DataRow dr =
dt.Rows[this.dataGridView1.SelectedCells[0].RowIndex];
// gives the same :
// DataRow dr =
dt.Rows[this.dataGridView1.SelectedCells[0].OwningRow.Index];
unfortunately looking at this.dataGridView1.SelectedCells[0].RowIndex
gives the index that would have been valid in the unsorted
datagridview. Is there a way of getting to the 'sorted' Index ? I have
tried calling this.dataGridView1.Refresh() but all to no avail. Thanks.