473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why must I rebind DataGrid to see Sort?

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
5 4429
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen | last post by:
I am trying to delete a row in a datagrid on the onclick of a asp:ButtonColumn. The datagrid is created from the items in an arraylist so what im trying to do is remove the item from the array and...
0
by: Andreas Klemt | last post by:
Hello, I have a UserControl in my DataGrid. When I rebind my DataGrid, the UserControls is gone. What is wrong? Thanks, Andreas
0
by: Robert Brinson | last post by:
Hello all! I'm running .NET Framework 1.1 using VS.NET 2003. I've got a mystery with a DataGrid. Below is the definition of the DataGrid from my aspx page: </asp:datagrid><asp:datagrid...
2
by: enak | last post by:
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added paging and when I display the dg it shows 5 pages. (I am showing page numbers at...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
1
by: jat14 | last post by:
h i am rather new to asp and this is probably a trivial question but.... i have a datagrid on a page showing some names. I have a button on this same page that when clicked i want to do some...
0
by: d pak | last post by:
Here is a snippit which replicates my issue. I have a datagrid which contains an input textbox on each row, binded on the serverside. However it seems that when I perform a postback to refresh teh...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.