PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Re: DataGrid: sorting and current row

Reply

Re: DataGrid: sorting and current row

 
Thread Tools Rate Thread
Old 25-06-2003, 07:03 AM   #1
Dmitriy Lapshin
Guest
 
Posts: n/a
Default Re: DataGrid: sorting and current row


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


  Reply With Quote
Old 25-06-2003, 11:53 AM   #2
Dennis McCarthy
Guest
 
Posts: n/a
Default Re: DataGrid: sorting and current row

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

>
>.
>

  Reply With Quote
Old 25-06-2003, 04:29 PM   #3
Dmitriy Lapshin
Guest
 
Posts: n/a
Default Re: DataGrid: sorting and current row

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


  Reply With Quote
Old 26-06-2003, 05:13 PM   #4
Dennis McCarthy
Guest
 
Posts: n/a
Default Re: DataGrid: sorting and current row

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

>
>.
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off