473,288 Members | 1,705 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,288 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 4425
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.