How to find which column is sorted in DataGrid control

G

Guest

I have in VB.NET 2005 a DataGrid control placed within a Windows form and the
property AllowSorting = True in order to allow users to further sort by any
column they want. The datagrid is based on a TableSet (not on a view).
How can I find during run-time at a certain moment if the DataGrid was
re-sorted by user by a certain column (and which is this column and if it is
sorted ascending or descending) or if datagrid was not re-sorted at all?

Thanks,
 
L

Linda Liu [MSFT]

Hi,

Based on my understanding, you're using DataGrid control in a WinForms
application written in VS2005. You'd like to know during run-time whether
the DataGrid control is resorted and if yes, which column is sorted,
ascending or descending. If I'm off base, please feel free to let me know.

I have spent some time researching on this question, but unfortunately I
didn't find how to get the above information when I bind a
DataSet/DataTable to a DataGrid control. The DataGrid control doesn't
expose such properties for us to determine whether the DataGrid control is
resorted, which column is sorted and the sort order. The DataTable class
doesn't supply such properties either. I have tried to appeal to the
default view of the DataTable ( we could get the default view of a
DataTable through its DefaultView property). However, DataView only has a
Sort property which is used to sort the underlying data source by a certain
rule.

I have also tried to use a BindingSource as the data source of the DataGrid
control. I could get whether the DataGrid control is resorted by the
IsSorted property of the BindingSource. But BindingSource couldn't tell me
the further information ( which column is sorted and the sort order).

Since you're using VS2005, I recommend you to use DataGridView instead of
DataGrid. DataGridView is a new control shipped with VS2005, which has many
new features and is apt to extend.

The DataGridView class has SortedColumn and SortOrder properties. If
there's no sort in the DataGridView control, the value of the SortedColumn
property is Nothing; otherwise, it refers to the currently sorted column
and the SortOrder property tells us the sort order.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi,

How abou the problem now?

If the problem is not resolved or you have anything unclear, please feel
free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

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