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

Disable Sorting in VB.NET

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

Dec 7 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
<si***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Dec 7 '05 #2

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

Dec 7 '05 #3

P: n/a
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.
<si***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Dec 7 '05 #4

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

Dec 7 '05 #5

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

Dec 7 '05 #6

P: n/a
Hi,

<si***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
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

Dec 7 '05 #7

P: n/a
Yes, that's why I asked. Sorry, I don't have any info. on the Windows Forms
DataGrid.
<si***********@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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.

Dec 7 '05 #8

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

Dec 7 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.