Why must I rebind DataGrid to see Sort?

B

Bill Todd

In a WinForms application with a DataGrid bound to a DataTable, if I
change the DefaultDataView.Sort property the order of the data in the
grid does not change.

If I change the Sort property then reassign the DataTable to the
DataGrid's DataSource the DataGrid does show the new order.

If I change the Sort property then call DataGrid.SetDataBinding the
DataGrid does _not_ show the new order.

1) Why must I rebind the grid to get it to display the data in the new
order?

2) Is rebinding the DataGrid the right (best) way to get it to display
the data in the new order?

3) Out of curiousity, why does SetDataBinding not work?

Thanks,
 
P

Peter Huang

Hi Bill,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to Sort the underlying
datatable of a datagrid in another order and you find that when you change
the datatable's defaultview the data in the datagrid will not be sorted.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I can not reproduce the problem.
Here is my test code.

DataSet ds = new DataSet("TestDS");
private void Form1_Load(object sender, System.EventArgs e)
{
System.Data.OleDb.OleDbConnection cn = new
System.Data.OleDb.OleDbConnection("<ConnectString>");
cn.Open();
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand("Select * from Employees");
cmd.Connection=cn;
System.Data.OleDb.OleDbDataAdapter da = new
System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
this.oleDbDataAdapter1.Fill(this.dataSet11);
this.dataGrid1.DataSource= ds.Tables[0];
}

private void button1_Click(object sender, System.EventArgs e)
{
this.ds.Tables[0].DefaultView.Sort = "City DESC";
}

You may try my code and let me know the result, if you still have any
concern can you post your code here, so that we can do further
troubleshooting.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi Bill,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to Sort the underlying
datatable of a datagrid in another order and you find that when you change
the datatable's defaultview the data in the datagrid will not be sorted.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I can not reproduce the problem.
Here is my test code.

DataSet ds = new DataSet("TestDS");
private void Form1_Load(object sender, System.EventArgs e)
{
System.Data.OleDb.OleDbConnection cn = new
System.Data.OleDb.OleDbConnection("<ConnectString>");
cn.Open();
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand("Select * from Employees");
cmd.Connection=cn;
System.Data.OleDb.OleDbDataAdapter da = new
System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
this.dataGrid1.DataSource= ds.Tables[0];
}

private void button1_Click(object sender, System.EventArgs e)
{
this.ds.Tables[0].DefaultView.Sort = "City DESC";
}

You may try my code and let me know the result, if you still have any
concern can you post your code here, so that we can do further
troubleshooting.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

William Ryan eMVP

Bill Todd said:
In a WinForms application with a DataGrid bound to a DataTable, if I
change the DefaultDataView.Sort property the order of the data in the
grid does not change.

If I change the Sort property then reassign the DataTable to the
DataGrid's DataSource the DataGrid does show the new order.

If I change the Sort property then call DataGrid.SetDataBinding the
DataGrid does _not_ show the new order.

1) Why must I rebind the grid to get it to display the data in the new
order?

You don't. there's something wrong if you have to.
2) Is rebinding the DataGrid the right (best) way to get it to display
the data in the new order?

You don't need to rebind so this shoudln't be an issue.
3) Out of curiousity, why does SetDataBinding not work?

Are you binding to the DataView? You mention binding to the datatable. you
can call sort all year on the View, that has NO effect on the datatable if
that's where the bind is happening.
 
B

Bill Todd

Thanks William and Peter. I read an article that stated that binding
to the DataTable actually binds to the default DataView. That is
obviously not the case. When I changed my code to bind to

DataSet.Tables["ATable"].DefaultView;

everything worked.
 
P

Peter Huang

Hi Bill,

I am glad to hear that the problem has been resolved.
If you have any concern on this issue, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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