473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 15041
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: steve | last post by:
Hi All How do I get the screen coordinates of the selected cell boundaries in a datagridview I need to position a form at the top left position of the selected cell regardless of where the...
2
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should...
0
by: jjsavage | last post by:
Hi all, What order are DataGridView.SelectedCells in? Is the cell the top-left-most, and the bottom-right-most? Thanks, John
0
bplacker
by: bplacker | last post by:
I have a datagrid with several columns which have visible = False. I also have navigation buttons, which do something like the following ( for moving to next row). Is this incorrect? and how do I...
5
by: DanThMan | last post by:
The situation: * I have a ButtonColumn in a DataGridView. * When the user preses one of the buttons, a dialog appears. * Based on what the user selects in the dialog, data is entered...
2
OuTCasT
by: OuTCasT | last post by:
Please can someone tell me how to add the totals of a single coloumn from a datagridview ? Private Sub UpdateLabelText() Dim SelectedCellTotal As Integer = 0 Dim...
6
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview bound it to sql table Throw Bindingsource To...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
1
by: gmcconville | last post by:
Im a little bit stuck on how to change a cell in a datagridview to bold without affecting any of the other attributes of the cell, such as underline, italic etc. This is what I thought would work...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.