469,928 Members | 1,862 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

ASP.NET DataGridView Sorting Problem

OK, this is rediculous...

Surely, sorting should be one of the easiest things with the ASP.NET DataGridView control. I mean, there's hundreds of sites out there (Microsoft MSDN entries included) which explain and give examples of the DataGridView sorting and how to make it work...


Apologies for the rant. I'll try and keep it to the point from here on in:

I have tried the following:
  • I have an object with a "ListComponents" function that I wrote to return a DataTable for my data source. When sorting didn't work using that, I tried...
  • ...researching it a bit, so I checked out the MSDN stuff (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortexpression. aspx) and basically copied the code (including the SqlDataSource), except substituding my connection string and SQL select statements where applicable...
  • ... This didn't work, so I went back to my original idea and tried mucking about with If Not IsPostBacks and various other things...
  • ... This didn't work, so I looked on the web some more, and everyone seems to be saying that it should just WORK!

I've had a good amount of success on this site, so I'm hoping someone will be able to help me.

Here is my code (for what it's worth)..:

ASP Code:
Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" ID="dgvComponents" runat="server">
  2.     <Columns>
  3.         <asp:BoundColumn HeaderText="ID" DataField="ID" Visible="False" ReadOnly="true" SortExpression="ID"></asp:BoundColumn>
  4.         <asp:BoundColumn HeaderText="Part Reference" DataField="SPARE_REF" ReadOnly="true" SortExpression="SPARE_REF"></asp:BoundColumn>
  5.         <asp:BoundColumn HeaderText="Part Name" DataField="NAME" ReadOnly="true" SortExpression="NAME"></asp:BoundColumn>
  6.     </Columns>
  7. </asp:DataGrid>
Code Behind:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  3.     dgvComponents.DataSource = Component.ListComponents()
  4.     dgvComponents.DataBind()
  6. End Sub
And, like, Component.ListComponents basically just returns a DataTable with the following as the select string:
Expand|Select|Wrap|Line Numbers
Incidentally, I also tried adding a "sortExpression" parameter to ListComponents, e.g.:
Expand|Select|Wrap|Line Numbers
  1. ListComponents("ORDER BY ID ASC")
When I mentioned that I was playing around with IsPostBack, here's an example:
Expand|Select|Wrap|Line Numbers
  1. If Not IsPostBack Then
  2.      dgvComponents.DataSource = Component.ListComponents()
  3. End If
(I thought perhaps I'm only supposed to set the DataSource first-time-round. This actually made the whole table disappear when I click a header...!

With regards to the results I get, basically, you click on a header (either NAME or PART REFERENCE) and nothing happens. I'd expect it to alternate between Ascending and Descending =)

Thanks for your time, in advance.

Oct 10 '08 #1
0 1315

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by mahesh.nimbalkar | last post: by
1 post views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
8 posts views Thread by Kyote | last post: by
4 posts views Thread by ReneMarxis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.