ASP.NET DataGridView Sorting Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Queez
    New Member
    • Jul 2007
    • 24

    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...

    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:
    Code:
    <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>
    Code Behind:
    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        dgvComponents.DataSource = Component.ListComponents()
        dgvComponents.DataBind()
    
    End Sub
    And, like, Component.ListC omponents basically just returns a DataTable with the following as the select string:
    Code:
    SELECT ID, NAME, SPARE_REF FROM COMPONENTS
    Incidentally, I also tried adding a "sortExpression " parameter to ListComponents, e.g.:
    Code:
    ListComponents("ORDER BY ID ASC")
    When I mentioned that I was playing around with IsPostBack, here's an example:
    Code:
    If Not IsPostBack Then
         dgvComponents.DataSource = Component.ListComponents()
    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.

    -Q
Working...