PC Review


Reply
Thread Tools Rate Thread

DataGrid and DataView question

 
 
=?Utf-8?B?VGltIEpvaG5zb24=?=
Guest
Posts: n/a
 
      9th Jun 2005
I inherited some code that binds a dataset/datatable to a datagrid. When the
user highlights some rows the code loops thru a DataView of the grid to act
on those rows. This works fine, unless you first click a column to sort by
that column. Then the datagrid rows are not in the same sequence as the
datasource table. I'm thinking this is a definite bug in this code, but I'm
wondering what the point of doing things this way might have been. I mean
why bother with CurrencyManager and DataView if you can just access the
elements via datagrid1[row, col]?

Here's an example of the loop which seems overkill to me (besides not
working if you sort first!):

CurrencyManager mgr = (CurrencyManager)
dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];

DataView dv = (DataView)mgr.List;

for (int i = 0; i < dv.Count; ++i)
{
if (dataGrid1.IsSelected(i))
{
DataRow row = dv.Table.Rows[i];

<get a field from row here...>
}
}

I mean, if the datagrid got sorted, the underlying datatable or dataview
isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
row 1 in the presorted datatable or dataview. Am I missing something in at
least the intention here?
 
Reply With Quote
 
 
 
 
Darren Shaffer
Guest
Posts: n/a
 
      9th Jun 2005
Tim,

There is no bug, you just need to understand the DataRowView and
CurrencyManager
behavior. Rather than write an essay here, instead I'll just give you the
code to make
sure your selection is correct after sorting. This code is for a DataGrid
that contains
a list of car dealerships.

private void dgDealership_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if ( dgDealership.VisibleRowCount == 0 )
return;

SortDataGrid(sender, e);

dgDealership.Select(dgDealership.CurrentRowIndex);
CurrencyManager cm =
(CurrencyManager)BindingContext[dgDealership.DataSource];

DataRowView drv = (DataRowView)cm.Current;

lblDealerName.Text = drv.Row[0].ToString();
lblDealerAddress.Text = drv.Row[1].ToString();

_dealerSelected = drv.Row[5].ToString();

}

public static void SortDataGrid(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hitTest;
DataTable dataTable;
DataView dataView;
string columnName;
DataGrid dataGrid;

if (e.Button == MouseButtons.Left)
{
dataGrid = (DataGrid)sender;
hitTest = dataGrid.HitTest(e.X, e.Y);
if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
{
dataTable = (DataTable)dataGrid.DataSource;
dataView = dataTable.DefaultView;

if(dataGrid.TableStyles.Count != 0)
columnName =
dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
else
columnName = dataTable.Columns[hitTest.Column].ColumnName;

if (dataView.Sort == columnName)
dataView.Sort = columnName + " DESC";
else
dataView.Sort = columnName;
}
}
}

--
Darren Shaffer
..NET Compact Framework MVP
Principal Architect
Connected Innovation
www.connectedinnovation.com



"Tim Johnson" <(E-Mail Removed)> wrote in message
news:E6741A3F-278C-4377-8DF5-(E-Mail Removed)...
>I inherited some code that binds a dataset/datatable to a datagrid. When
>the
> user highlights some rows the code loops thru a DataView of the grid to
> act
> on those rows. This works fine, unless you first click a column to sort
> by
> that column. Then the datagrid rows are not in the same sequence as the
> datasource table. I'm thinking this is a definite bug in this code, but
> I'm
> wondering what the point of doing things this way might have been. I mean
> why bother with CurrencyManager and DataView if you can just access the
> elements via datagrid1[row, col]?
>
> Here's an example of the loop which seems overkill to me (besides not
> working if you sort first!):
>
> CurrencyManager mgr = (CurrencyManager)
> dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
>
> DataView dv = (DataView)mgr.List;
>
> for (int i = 0; i < dv.Count; ++i)
> {
> if (dataGrid1.IsSelected(i))
> {
> DataRow row = dv.Table.Rows[i];
>
> <get a field from row here...>
> }
> }
>
> I mean, if the datagrid got sorted, the underlying datatable or dataview
> isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
> row 1 in the presorted datatable or dataview. Am I missing something in
> at
> least the intention here?



 
Reply With Quote
 
=?Utf-8?B?VGltIEpvaG5zb24=?=
Guest
Posts: n/a
 
      9th Jun 2005
Thanks, I see what you're doing. I'd say it is a bug in the code I inherited
though since the person didn't manage the sorting himself! I'll adjust the
code as you indicated and all the objects will sort together.

"Darren Shaffer" wrote:

> Tim,
>
> There is no bug, you just need to understand the DataRowView and
> CurrencyManager
> behavior. Rather than write an essay here, instead I'll just give you the
> code to make
> sure your selection is correct after sorting. This code is for a DataGrid
> that contains
> a list of car dealerships.
>
> private void dgDealership_MouseUp(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> if ( dgDealership.VisibleRowCount == 0 )
> return;
>
> SortDataGrid(sender, e);
>
> dgDealership.Select(dgDealership.CurrentRowIndex);
> CurrencyManager cm =
> (CurrencyManager)BindingContext[dgDealership.DataSource];
>
> DataRowView drv = (DataRowView)cm.Current;
>
> lblDealerName.Text = drv.Row[0].ToString();
> lblDealerAddress.Text = drv.Row[1].ToString();
>
> _dealerSelected = drv.Row[5].ToString();
>
> }
>
> public static void SortDataGrid(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> DataGrid.HitTestInfo hitTest;
> DataTable dataTable;
> DataView dataView;
> string columnName;
> DataGrid dataGrid;
>
> if (e.Button == MouseButtons.Left)
> {
> dataGrid = (DataGrid)sender;
> hitTest = dataGrid.HitTest(e.X, e.Y);
> if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
> {
> dataTable = (DataTable)dataGrid.DataSource;
> dataView = dataTable.DefaultView;
>
> if(dataGrid.TableStyles.Count != 0)
> columnName =
> dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
> else
> columnName = dataTable.Columns[hitTest.Column].ColumnName;
>
> if (dataView.Sort == columnName)
> dataView.Sort = columnName + " DESC";
> else
> dataView.Sort = columnName;
> }
> }
> }
>
> --
> Darren Shaffer
> ..NET Compact Framework MVP
> Principal Architect
> Connected Innovation
> www.connectedinnovation.com
>
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:E6741A3F-278C-4377-8DF5-(E-Mail Removed)...
> >I inherited some code that binds a dataset/datatable to a datagrid. When
> >the
> > user highlights some rows the code loops thru a DataView of the grid to
> > act
> > on those rows. This works fine, unless you first click a column to sort
> > by
> > that column. Then the datagrid rows are not in the same sequence as the
> > datasource table. I'm thinking this is a definite bug in this code, but
> > I'm
> > wondering what the point of doing things this way might have been. I mean
> > why bother with CurrencyManager and DataView if you can just access the
> > elements via datagrid1[row, col]?
> >
> > Here's an example of the loop which seems overkill to me (besides not
> > working if you sort first!):
> >
> > CurrencyManager mgr = (CurrencyManager)
> > dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
> >
> > DataView dv = (DataView)mgr.List;
> >
> > for (int i = 0; i < dv.Count; ++i)
> > {
> > if (dataGrid1.IsSelected(i))
> > {
> > DataRow row = dv.Table.Rows[i];
> >
> > <get a field from row here...>
> > }
> > }
> >
> > I mean, if the datagrid got sorted, the underlying datatable or dataview
> > isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
> > row 1 in the presorted datatable or dataview. Am I missing something in
> > at
> > least the intention here?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?VGltIEpvaG5zb24=?=
Guest
Posts: n/a
 
      23rd Jul 2005
Two followup questions. Here's the first:

I can see the underlying dataview being sorted when I use your code, so when
I click on column "XYZ" I see it sorted ascending when I stop in the
debugger. When I let it go however it ends up sorted DEscending. It's as
though I've sorted the underlying source, which the grid sticks with, then
afterwards the grid control does its own sort. And since by the time he sees
it he thinks the click on column XYZ is a 2nd sort. How can I override the
datagrid's "extra" sort, if in fact that's what's happeing?

Tim

"Darren Shaffer" wrote:

> Tim,
>
> There is no bug, you just need to understand the DataRowView and
> CurrencyManager
> behavior. Rather than write an essay here, instead I'll just give you the
> code to make
> sure your selection is correct after sorting. This code is for a DataGrid
> that contains
> a list of car dealerships.
>
> private void dgDealership_MouseUp(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> if ( dgDealership.VisibleRowCount == 0 )
> return;
>
> SortDataGrid(sender, e);
>
> dgDealership.Select(dgDealership.CurrentRowIndex);
> CurrencyManager cm =
> (CurrencyManager)BindingContext[dgDealership.DataSource];
>
> DataRowView drv = (DataRowView)cm.Current;
>
> lblDealerName.Text = drv.Row[0].ToString();
> lblDealerAddress.Text = drv.Row[1].ToString();
>
> _dealerSelected = drv.Row[5].ToString();
>
> }
>
> public static void SortDataGrid(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> DataGrid.HitTestInfo hitTest;
> DataTable dataTable;
> DataView dataView;
> string columnName;
> DataGrid dataGrid;
>
> if (e.Button == MouseButtons.Left)
> {
> dataGrid = (DataGrid)sender;
> hitTest = dataGrid.HitTest(e.X, e.Y);
> if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
> {
> dataTable = (DataTable)dataGrid.DataSource;
> dataView = dataTable.DefaultView;
>
> if(dataGrid.TableStyles.Count != 0)
> columnName =
> dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
> else
> columnName = dataTable.Columns[hitTest.Column].ColumnName;
>
> if (dataView.Sort == columnName)
> dataView.Sort = columnName + " DESC";
> else
> dataView.Sort = columnName;
> }
> }
> }
>
> --
> Darren Shaffer
> ..NET Compact Framework MVP
> Principal Architect
> Connected Innovation
> www.connectedinnovation.com
>
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:E6741A3F-278C-4377-8DF5-(E-Mail Removed)...
> >I inherited some code that binds a dataset/datatable to a datagrid. When
> >the
> > user highlights some rows the code loops thru a DataView of the grid to
> > act
> > on those rows. This works fine, unless you first click a column to sort
> > by
> > that column. Then the datagrid rows are not in the same sequence as the
> > datasource table. I'm thinking this is a definite bug in this code, but
> > I'm
> > wondering what the point of doing things this way might have been. I mean
> > why bother with CurrencyManager and DataView if you can just access the
> > elements via datagrid1[row, col]?
> >
> > Here's an example of the loop which seems overkill to me (besides not
> > working if you sort first!):
> >
> > CurrencyManager mgr = (CurrencyManager)
> > dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
> >
> > DataView dv = (DataView)mgr.List;
> >
> > for (int i = 0; i < dv.Count; ++i)
> > {
> > if (dataGrid1.IsSelected(i))
> > {
> > DataRow row = dv.Table.Rows[i];
> >
> > <get a field from row here...>
> > }
> > }
> >
> > I mean, if the datagrid got sorted, the underlying datatable or dataview
> > isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
> > row 1 in the presorted datatable or dataview. Am I missing something in
> > at
> > least the intention here?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?VGltIEpvaG5zb24=?=
Guest
Posts: n/a
 
      23rd Jul 2005
Here's the second one:

I implemented the Sort logic so that when you click a column header to sort,
the grid and the dataset stay in sync. But when I attempt to pick off a
column value of a selected row I get two different results depending on which
statement I use (the "bad" one was in some legacy code; the "good" one is
based on your email).

First I get the DataView and selected row index:

CurrencyManager cm = (CurrencyManager) dg.BindingContext[dg.DataSource,
dg.DataMember];
DataView dv = (DataView)cm.List;
int index = dg.CurrentRowIndex;

Then this gets me the "right" value from the newly sorted list:
DataRowView drv = dv[index];
string s = drv["myfield"].ToString();

This gets me the "old" value from the original unsorted list:
DataRowdr = dv.Table.Rows[index];
string s = dr["myfield"].ToString();

Basically, how does dv[i] relate to dv.Table.Rows[i] when there's a Sort
property set? Are there two dataviews at work here somehow?


"Darren Shaffer" wrote:

> Tim,
>
> There is no bug, you just need to understand the DataRowView and
> CurrencyManager
> behavior. Rather than write an essay here, instead I'll just give you the
> code to make
> sure your selection is correct after sorting. This code is for a DataGrid
> that contains
> a list of car dealerships.
>
> private void dgDealership_MouseUp(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> if ( dgDealership.VisibleRowCount == 0 )
> return;
>
> SortDataGrid(sender, e);
>
> dgDealership.Select(dgDealership.CurrentRowIndex);
> CurrencyManager cm =
> (CurrencyManager)BindingContext[dgDealership.DataSource];
>
> DataRowView drv = (DataRowView)cm.Current;
>
> lblDealerName.Text = drv.Row[0].ToString();
> lblDealerAddress.Text = drv.Row[1].ToString();
>
> _dealerSelected = drv.Row[5].ToString();
>
> }
>
> public static void SortDataGrid(object sender,
> System.Windows.Forms.MouseEventArgs e)
> {
> DataGrid.HitTestInfo hitTest;
> DataTable dataTable;
> DataView dataView;
> string columnName;
> DataGrid dataGrid;
>
> if (e.Button == MouseButtons.Left)
> {
> dataGrid = (DataGrid)sender;
> hitTest = dataGrid.HitTest(e.X, e.Y);
> if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
> {
> dataTable = (DataTable)dataGrid.DataSource;
> dataView = dataTable.DefaultView;
>
> if(dataGrid.TableStyles.Count != 0)
> columnName =
> dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
> else
> columnName = dataTable.Columns[hitTest.Column].ColumnName;
>
> if (dataView.Sort == columnName)
> dataView.Sort = columnName + " DESC";
> else
> dataView.Sort = columnName;
> }
> }
> }
>
> --
> Darren Shaffer
> ..NET Compact Framework MVP
> Principal Architect
> Connected Innovation
> www.connectedinnovation.com
>
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:E6741A3F-278C-4377-8DF5-(E-Mail Removed)...
> >I inherited some code that binds a dataset/datatable to a datagrid. When
> >the
> > user highlights some rows the code loops thru a DataView of the grid to
> > act
> > on those rows. This works fine, unless you first click a column to sort
> > by
> > that column. Then the datagrid rows are not in the same sequence as the
> > datasource table. I'm thinking this is a definite bug in this code, but
> > I'm
> > wondering what the point of doing things this way might have been. I mean
> > why bother with CurrencyManager and DataView if you can just access the
> > elements via datagrid1[row, col]?
> >
> > Here's an example of the loop which seems overkill to me (besides not
> > working if you sort first!):
> >
> > CurrencyManager mgr = (CurrencyManager)
> > dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
> >
> > DataView dv = (DataView)mgr.List;
> >
> > for (int i = 0; i < dv.Count; ++i)
> > {
> > if (dataGrid1.IsSelected(i))
> > {
> > DataRow row = dv.Table.Rows[i];
> >
> > <get a field from row here...>
> > }
> > }
> >
> > I mean, if the datagrid got sorted, the underlying datatable or dataview
> > isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
> > row 1 in the presorted datatable or dataview. Am I missing something in
> > at
> > least the intention here?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?VGltIEpvaG5zb24=?=
Guest
Posts: n/a
 
      23rd Jul 2005
Doh! My mistake on the 2nd question - obviously the dv.Table reference puts
you back in the original (unsorted) DataTable so the Sort property doesn't
apply. Never mind!

"Tim Johnson" wrote:

> Here's the second one:
>
> I implemented the Sort logic so that when you click a column header to sort,
> the grid and the dataset stay in sync. But when I attempt to pick off a
> column value of a selected row I get two different results depending on which
> statement I use (the "bad" one was in some legacy code; the "good" one is
> based on your email).
>
> First I get the DataView and selected row index:
>
> CurrencyManager cm = (CurrencyManager) dg.BindingContext[dg.DataSource,
> dg.DataMember];
> DataView dv = (DataView)cm.List;
> int index = dg.CurrentRowIndex;
>
> Then this gets me the "right" value from the newly sorted list:
> DataRowView drv = dv[index];
> string s = drv["myfield"].ToString();
>
> This gets me the "old" value from the original unsorted list:
> DataRowdr = dv.Table.Rows[index];
> string s = dr["myfield"].ToString();
>
> Basically, how does dv[i] relate to dv.Table.Rows[i] when there's a Sort
> property set? Are there two dataviews at work here somehow?
>
>
> "Darren Shaffer" wrote:
>
> > Tim,
> >
> > There is no bug, you just need to understand the DataRowView and
> > CurrencyManager
> > behavior. Rather than write an essay here, instead I'll just give you the
> > code to make
> > sure your selection is correct after sorting. This code is for a DataGrid
> > that contains
> > a list of car dealerships.
> >
> > private void dgDealership_MouseUp(object sender,
> > System.Windows.Forms.MouseEventArgs e)
> > {
> > if ( dgDealership.VisibleRowCount == 0 )
> > return;
> >
> > SortDataGrid(sender, e);
> >
> > dgDealership.Select(dgDealership.CurrentRowIndex);
> > CurrencyManager cm =
> > (CurrencyManager)BindingContext[dgDealership.DataSource];
> >
> > DataRowView drv = (DataRowView)cm.Current;
> >
> > lblDealerName.Text = drv.Row[0].ToString();
> > lblDealerAddress.Text = drv.Row[1].ToString();
> >
> > _dealerSelected = drv.Row[5].ToString();
> >
> > }
> >
> > public static void SortDataGrid(object sender,
> > System.Windows.Forms.MouseEventArgs e)
> > {
> > DataGrid.HitTestInfo hitTest;
> > DataTable dataTable;
> > DataView dataView;
> > string columnName;
> > DataGrid dataGrid;
> >
> > if (e.Button == MouseButtons.Left)
> > {
> > dataGrid = (DataGrid)sender;
> > hitTest = dataGrid.HitTest(e.X, e.Y);
> > if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
> > {
> > dataTable = (DataTable)dataGrid.DataSource;
> > dataView = dataTable.DefaultView;
> >
> > if(dataGrid.TableStyles.Count != 0)
> > columnName =
> > dataGrid.TableStyles[0].GridColumnStyles[hitTest.Column].MappingName;
> > else
> > columnName = dataTable.Columns[hitTest.Column].ColumnName;
> >
> > if (dataView.Sort == columnName)
> > dataView.Sort = columnName + " DESC";
> > else
> > dataView.Sort = columnName;
> > }
> > }
> > }
> >
> > --
> > Darren Shaffer
> > ..NET Compact Framework MVP
> > Principal Architect
> > Connected Innovation
> > www.connectedinnovation.com
> >
> >
> >
> > "Tim Johnson" <(E-Mail Removed)> wrote in message
> > news:E6741A3F-278C-4377-8DF5-(E-Mail Removed)...
> > >I inherited some code that binds a dataset/datatable to a datagrid. When
> > >the
> > > user highlights some rows the code loops thru a DataView of the grid to
> > > act
> > > on those rows. This works fine, unless you first click a column to sort
> > > by
> > > that column. Then the datagrid rows are not in the same sequence as the
> > > datasource table. I'm thinking this is a definite bug in this code, but
> > > I'm
> > > wondering what the point of doing things this way might have been. I mean
> > > why bother with CurrencyManager and DataView if you can just access the
> > > elements via datagrid1[row, col]?
> > >
> > > Here's an example of the loop which seems overkill to me (besides not
> > > working if you sort first!):
> > >
> > > CurrencyManager mgr = (CurrencyManager)
> > > dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
> > >
> > > DataView dv = (DataView)mgr.List;
> > >
> > > for (int i = 0; i < dv.Count; ++i)
> > > {
> > > if (dataGrid1.IsSelected(i))
> > > {
> > > DataRow row = dv.Table.Rows[i];
> > >
> > > <get a field from row here...>
> > > }
> > > }
> > >
> > > I mean, if the datagrid got sorted, the underlying datatable or dataview
> > > isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
> > > row 1 in the presorted datatable or dataview. Am I missing something in
> > > at
> > > least the intention here?

> >
> >
> >

 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using DataView in DataGrid Yossi And Inbar Microsoft Dot NET Framework Forms 0 16th Jan 2006 09:39 PM
DataView, DataGrid and IsPostBack enceladus311@yahoo.com Microsoft ASP .NET 5 10th Jan 2006 06:21 PM
Is there an easy way to copy a DataView (or even the DataGrid showing the DataView) to the Clipboard? Kevin Brown Microsoft Dot NET 4 5th Jan 2005 09:01 PM
Using DataView with a datagrid Hananiel Microsoft C# .NET 2 8th Jan 2004 06:28 PM
Newbie Question - DataSet/DataView (DataGrid?) e_malinger Microsoft ADO .NET 3 16th Sep 2003 07:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.