Disable Sorting in VB.NET

S

simchajoy2000

I thought the only thing I had to do to disable column sorting in
VB.NET was to set datagrid.AllowSorting = False. Unfortunately this
has never worked for me. I discovered another set of code that seems
to work for 99% of the cases where I need to disable datagrid column
sorting:

Private Sub datagrid_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles datagrid.MouseDown
Dim pt As New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dgACE.HitTest(pt)

If hti.Type <> DataGrid.HitTestType.ColumnHeader Then
MyBase.OnMouseDown(e)
End If
End Sub

However, this code does not work when my datagrid resides on a user
control. The datagrid_MouseDown event is triggered but sorting still
occurs for the column. It's critical that I disable sorting in the
datagrid due to the other things I am trying to do with the datagrid.

I'm about ready to pull my hair out trying to figure this one out so
any suggestions on how to resolve this issue would be greatly
appreciated!

Thank you!

Joy
 
S

Scott M.

This problem sounds very strange since the DEFAULT of a DataGrid is NO
SORTING. In other words, you must change a property value AND write code in
the DataGrid's SortCommand event handler as well as define the sort criteria
for each column of the DataGrid for sorting to work.

I find it hard to believe that you get sorting "automatically" without doing
any of these things.
 
S

simchajoy2000

When I talk about sorting, I am referring to the ability to click on a
datagrid columnheader and sort the specified column by ascending or
descending order.

I can tell you that I have no SortCommand event handlers whatsoever and
perhaps it is strange that it is happening by default but for whatever
reason that is what is happening. I wouldn't have posted this question
if I was experiencing what you are suggesting.

But perhaps I can use the SortCommand event handler to disable this
functionality.
 
S

Scott M.

What EXACT control are you using (Windows or Web Form DataGrid)?
What Framework/VS.NET are you talking about?

In VS.NET 2003 (Framework 1.1), there are 3 things that must be done to get
a Web Form DataGrid to sort (and none of these are done by default):

The DataGrid must have its EnableSort property turned on (default is off).
The columns to be sortable, must have a sort field specified (not done by
default).
The DataGrid's SortCommand event must contain the code that actaully
performs the sort (not done by default).

This is all I can tell you. Good luck.
 
S

simchajoy2000

I am using the Windows DataGrid with VS.NET 2003.

1. First, when I type "datagridname." the intellisense does not display
the EnableSort property you mention. (fyi. I create a new instance of
a datagrid by simply dragging and dropping it from the Toolbar onto the
form.) So I have no idea where this mysterious property you speak of
resides. There is an AllowSorting property I mentioned previously but
like I said, it seems to sort no matter if this property is set or not.
In fact, when I search for "datagrid EnableSort" in the VS.NET help
topics, it returns 0 topics. Neither can I find anything in
www.msdn.com on "datagrid EnableSort" - I am still wading through the
results for "datagrid sort" and haven't found anything yet. So if you
could refer me to some documentation on "EnableSort" I would greatly
appreciate it.

2. Second, I looked and I can find no handler or overridable event
method called "SortCommand" (or by any other name for that matter) for
the datagrid.

3. Third, the previous code I mentioned to disable the sorting
functionality of a VB.NET datagrid did not come out of my own head. I
found it in google groups. So if what you are saying is true, why
wouldn't they have simply set EnableSort = false? Therefore I have a
feeling that I am not alone when it comes to trying to disable the
sorting functionality.

If anyone else has any suggestions or has encountered a similar problem
I would greatly appreciate any suggested fixes! Thank you.

Joy
 
S

simchajoy2000

When I did some more searching I was able to find the SortCommand for
the WebControl datagrid - however, I am working with the Windows Forms
datagrid and that method does not exist for that control.
 
B

Bart Mermuys

Hi,

I thought the only thing I had to do to disable column sorting in
VB.NET was to set datagrid.AllowSorting = False. Unfortunately this
has never worked for me.

What kind of DataSource are you using because i can't reproduce the problem.
Setting DataGrid.AllowSorting to false does seem to work, but if you use
DataGridTableStyle(s) then you must set the relevant
DataGridTableStyle.AllowSorting to false.

HTH,
Greetings
 
S

Scott M.

Yes, that's why I asked. Sorry, I don't have any info. on the Windows Forms
DataGrid.
 
S

simchajoy2000

Thanks Bart - that did the trick! I was neglecting to set the
DataGridTableStyle.AllowSorting to False, I was only doing it for the
datagrid. Thanks a lot! I'm glad it was such a simple fix!

Joy
 

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