DataGridView.SelectedCells returning the Cell/Index in the Unsorted Datatable

C

clogg02

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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;
 
C

clogg02

On Oct 25, 10:04 am, "Nicholas Paldino [.NET/C# MVP]"

Thanks a lot! That did the trick.
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]
- (e-mail address removed)


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.
 

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