Re: DataGrid: sorting and current row

D

Dmitriy Lapshin

Hi Dennis,

That's a piece of code that seems to do the job:

CurrencyManager cm =
(CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember];
DataView theView = (DataView)cm.List;

DataRow currentRow = theView[this.dataGrid1.CurrentRowIndex].Row;
 
D

Dennis McCarthy

Dmitriy,

That does not seem to work either. When I put a
breakpoint in the code and examine the various objects,
it turns out that the CurrentRowIndex is not tracking the
selection in the DataGrid. It always has the same value.
Strangely, the protected currentRow member of the DataGrid
does track the selection properly, but I can't access it
from my code.

Any other ideas?

Thanks,
Dennis
-----Original Message-----
Hi Dennis,

That's a piece of code that seems to do the job:

CurrencyManager cm =
(CurrencyManager)this.dataGrid1.BindingContext [this.dataGrid1.DataSource,
this.dataGrid1.DataMember];
DataView theView = (DataView)cm.List;

DataRow currentRow = theView [this.dataGrid1.CurrentRowIndex].Row;

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software


I have a DataGrid that is bound to a DataTable in a typed
DataSet. The following code is used to get the current
row in the DataGrid, even after the user has clicked on a
column header to sort:

BindingManagerBase bm = this.dataGrid1.BindingContext
[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
DataRow dr = ((DataRowView)bm.Current).Row;

I would like to have the DataGrid initially be sorted,
just as if the user had clicked on the first column
header. I tried doing this by binding the DataGrid to
sorted DataView.

DataView view = new DataView (MyDataSet.MyDataTable);
view.Sort = "NAME";
dataGrid1.DataSource = view;

This works for sorting, but breaks the logic above for
getting the current row. No matter what row the user
selects, the binding manager always has the same current
row.

How can I pre-sort my DataGrid without losing the ability
to track the current row?

Thanks,
Dennis

.
 
D

Dmitriy Lapshin

Dennis,

I haven't had any problems with CurrentRowIndex provided you query for its
value in a proper place. If you could elaborate on which events you handle
and where you check CurrentRowIndex, I could probably be more helpful.

However...
Strangely, the protected currentRow member of the DataGrid
does track the selection properly, but I can't access it
from my code.

If this is so, you can inherit from the DataGrid control, thus making this
member accessible from your code and rely on it.

Yet another idea is to check for CurrencyManager's Position property value.
 
D

Dennis McCarthy

Dmitriy,

The code that gets the current row for the data grid:

BindingManagerBase bm = this.dataGrid1.BindingContext
[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
DataRow dr = ((DataRowView)bm.Current).Row;

executes in the event handler for a menu item. The data
set has been populated and the data grid bound before the
user selects the menu item.

I hope that helps.

Dennis
-----Original Message-----
Dennis,

I haven't had any problems with CurrentRowIndex provided you query for its
value in a proper place. If you could elaborate on which events you handle
and where you check CurrentRowIndex, I could probably be more helpful.

However...
Strangely, the protected currentRow member of the DataGrid
does track the selection properly, but I can't access it
from my code.

If this is so, you can inherit from the DataGrid control, thus making this
member accessible from your code and rely on it.

Yet another idea is to check for CurrencyManager's Position property value.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

Dmitriy,

That does not seem to work either. When I put a
breakpoint in the code and examine the various objects,
it turns out that the CurrentRowIndex is not tracking the
selection in the DataGrid. It always has the same value.
Strangely, the protected currentRow member of the DataGrid
does track the selection properly, but I can't access it
from my code.

Any other ideas?

Thanks,
Dennis

.
 

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