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...
SO WHY CAN'T I GET IT TO WORK?! Dammit!
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.w ebcontrols.grid view.sortexpres sion.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
- <asp:DataGrid AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" ID="dgvComponents" runat="server">
- <Columns>
- <asp:BoundColumn HeaderText="ID" DataField="ID" Visible="False" ReadOnly="true" SortExpression="ID"></asp:BoundColumn>
- <asp:BoundColumn HeaderText="Part Reference" DataField="SPARE_REF" ReadOnly="true" SortExpression="SPARE_REF"></asp:BoundColumn>
- <asp:BoundColumn HeaderText="Part Name" DataField="NAME" ReadOnly="true" SortExpression="NAME"></asp:BoundColumn>
- </Columns>
- </asp:DataGrid>
Expand|Select|Wrap|Line Numbers
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- dgvComponents.DataSource = Component.ListComponents()
- dgvComponents.DataBind()
- End Sub
Expand|Select|Wrap|Line Numbers
- SELECT ID, NAME, SPARE_REF FROM COMPONENTS
Expand|Select|Wrap|Line Numbers
- ListComponents("ORDER BY ID ASC")
Expand|Select|Wrap|Line Numbers
- If Not IsPostBack Then
- dgvComponents.DataSource = Component.ListComponents()
- End If
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.
-Q