PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Re: DataGrid: sorting and current row
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Re: DataGrid: sorting and current row
![]() |
Re: DataGrid: sorting and current row |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 "Dennis McCarthy" <Dennis_McCarthy@onesource.com> wrote in message news:00c401c33a7c$7ae6fd20$a501280a@phx.gbl... > 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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 > > >"Dennis McCarthy" <Dennis_McCarthy@onesource.com> wrote in message >news:00c401c33a7c$7ae6fd20$a501280a@phx.gbl... >> 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 > >. > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 "Dennis McCarthy" <Dennis_McCarthy@onesource.com> wrote in message news:049501c33b10$5f7e8af0$a001280a@phx.gbl... > 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 |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 > >"Dennis McCarthy" <Dennis_McCarthy@onesource.com> wrote in message >news:049501c33b10$5f7e8af0$a001280a@phx.gbl... >> 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 > >. > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

