By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Why must I rebind DataGrid to see Sort?

P: n/a
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,
--
Bill
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Nov 16 '05 #2

P: n/a
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.

Nov 16 '05 #3

P: n/a

"Bill Todd" <no@no.com> wrote in message
news:0p********************************@4ax.com...
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.
Thanks,
--
Bill

Nov 16 '05 #4

P: n/a
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.
--
Bill
Nov 16 '05 #5

P: n/a
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.

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.