locked
gridView autosorting with code-behind RRS feed

  • Question

  • User-314127261 posted

    If I fully define a gridview and its associated datasource in the aspx file, I have no problem getting the grid to autosort. 

    When I move the much of the definition of the grid and datasource to codebehind to be generated dynamically (i.e. to use a single gridview on a page for several purposes depending on what the user requests), auto sort appears to happen after clicking a sort link, but I wind up with an empty grid.

    The followng is defined in my aspx:
    <asp:GridView  ID="grdRoster" runat="server" AllowSorting="True" EnableViewState="True">
             <RowStyle CssClass="rowodd" />
             <AlternatingRowStyle CssClass="roweven" />
    </asp:GridView>
    <br />
    <asp:sqldatasource id="ds" runat=server EnableViewState="True"/>

    In my codebehind sub that populates the gridview I:
    1. create a sql statement string
    2. set datasource parameters:
           With ds
                .ConnectionString = getConnectionString()
                .SelectCommand = sql
                .DataSourceMode = SqlDataSourceMode.DataSet
            End With
    3. build out my gridView columns (including sort expressions) based on a list (fieldList) of display fields
    4. set and bind the gridView, and make it visible:
            With grdRoster
                .DataSourceID = ds.ID.ToString
                .EnableViewState = True
                .AllowSorting = True
                If fieldList.Trim = "" Then
                    .AutoGenerateColumns = True
                Else
                    .AutoGenerateColumns = False
                End If
                .DataBind()
            End With
            grdRoster.Visible = True

    So far so good. The gridView displays the data in the sort order specified in my starting sql statement.

    However, when I click the sort button, the result is an empty gridview.  As I trace the request, the gridview.Sorting method shows the correct number of rows in the sender and the expected sorting parameters in the event arguments (e).  By the time I get to the gridview.preRendering and Unload methods, the gridview has 0 rows.  Note that the gridview data survives other page postbacks generated by controls outside of the gridview, just not the sort link postback.

    If I subsequently run the sub that builds the gridview in the first place, it will be sorted  by the last sortlink click I made.  What I am left with is having to rerun this sub from the gridview.Sorting method to get my data back in the gridview. This gets my page to where I want it to be with minimal code, but my understanding was that  this should have happened without a subsequent need to call a routine that rebinds the gridview to the datasource. 

    Any help or advice appreciated! I have only found one reference to this issue, and every tutorial or example I have seen has both the datasource and gridview fully defined on the ASPX page rather than in code-behind.

    Saturday, October 14, 2006 6:44 PM

Answers