471,122 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

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

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.

Oct 25 '07 #1
2 14889
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.

Oct 25 '07 #2
On Oct 25, 10:04 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:

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]
- m...@spam.guard.caspershouse.com

"clogg02" <crog...@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.

Oct 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by jjsavage | last post: by
OuTCasT
2 posts views Thread by OuTCasT | last post: by
6 posts views Thread by =?Utf-8?B?TU1TSkVE?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.