Sorting a GridView in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kurt Jakobsen
    New Member
    • Jun 2007
    • 38

    Sorting a GridView in C#

    Hello,
    does anybody have a simple and good example on how to sort any of the columns in a gridView? Preferrable a C# example.
    How does it go along with a default paging handler?

    My paging event handler looks like this:

    protected void GridCtrlSite_In dexChange(objec t sender, GridViewPageEve ntArgs e)
    {
    g_GridCtrlSite. DataSource = (DataSet)Sessio n["Site_DataS et"];
    g_GridCtrlSite. PageIndex = e.NewPageIndex;
    g_GridCtrlSite. DataBind();
    }


    Best regards
    Kurt
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is an article that may help:
    GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data

    Comment

    • Kurt Jakobsen
      New Member
      • Jun 2007
      • 38

      #3
      Thank you kenobewan,
      I read through the article and it looks to me like my code should be conform with it, but for some reason it still does not work. The colum headers are "click able", but when they are clicked, sorting is not performed.

      Maybe there is something with my control config or config files or maybe I need something in the onsorting handler or maybe I need a different handler? I'm quite stuck here. Appreciate it if you have more input to guide me further.

      Below I've pasted my code, web.config and my theme / skin file.

      best regards
      Kurt

      Code:

      void FillInGridView( )
      {
      string strSQL = "SELECT BatchNo, QtyStarted FROM UnitsStarted;";
      string myConnString = ConfigurationSe ttings.AppSetti ngs["DSN_XXXX"];
      MySqlConnection myConnection = new MySqlConnection (myConnString);
      MySqlDataAdapte r myDataAdapter = new MySqlDataAdapte r(strSQL, myConnection);
      DataSet myDataSet = new DataSet();
      myDataAdapter.F ill(myDataSet, "BatchList" );
      g_GridCtrlBatch .DataSource = myDataSet;
      g_GridCtrlBatch .DataBind();
      myConnection.Cl ose();
      }


      void GridView_Sortin g(Object sender, GridViewSortEve ntArgs e)
      {
      }


      Control config:
      <asp:GridView ID="g_GridCtrlB atch" runat="server" DataKeyNames="B atchNo" OnPageIndexChan ging="BatchGrid Ctrl_IndexChang e" PageSize="20" AutoGenerateCol umns="False" onsorting="Grid View_Sorting">
      <Columns>
      <asp:TemplateFi eld>
      <ItemTemplate >
      <asp:Button ID="b_g_SelectB atc" runat="server" OnClick="EditBa tchRow" Text="Select" />
      </ItemTemplate>
      </asp:TemplateFie ld>
      <asp:BoundFie ld ReadOnly="true" HeaderText="Bat chNo" DataField="Batc hNo" SortExpression= "BatchNo" />
      <asp:BoundFie ld ReadOnly="true" HeaderText="Qty Started" DataField="QtyS tarted" SortExpression= "QtyStarted " />
      </Columns>
      </asp:GridView></td>



      Web.config
      <configuratio n>
      <appSettings>
      <add key="DSN_XXXX" value="server=Y YYY; user id=ZZZZ; password=AAAA; database=BBBB; pooling=false;"/>
      </appSettings>
      <system.web>
      <compilation debug="true"/>
      <customErrors mode="Off"/>
      <pages theme="CCCCThem e"
      enableEventVali dation="false"
      compilationMode ="Always"
      maintainScrollP ositionOnPostBa ck="true"
      asyncTimeout="4 50">
      <namespaces>
      </namespaces>
      <tagMapping>
      </tagMapping>
      </pages>
      <sessionState
      mode="InProc"
      stateConnection String="tcpip=1 27.0.0.1:42424"
      sqlConnectionSt ring="data source=127.0.0. 1;Trusted_Conne ction=yes"
      cookieless="fal se"
      timeout="640"
      />
      </system.web>
      </configuration>


      CCCCTheme.skin
      <asp:DropDownLi st runat="server" BackColor="beig e" ForeColor="blac k" font-size="XX-Small"/>
      <asp:TextBox runat="server" BackColor="beig e" ForeColor="blac k" font-size="X-Small"/>
      <asp:GridView runat="server" BackColor="beig e" ForeColor="blac k" font-size="X-Small" CssClass="DataW ebControlStyle" EnableSortingAn dPagingCallback s="false" AllowSorting="t rue" AllowPaging="tr ue">
      <AlternatingRow Style CssClass="Alter natingRowStyle" />
      <RowStyle CssClass="RowSt yle" />
      <HeaderStyle CssClass="Heade rStyle" />
      <FooterStyle CssClass="Foote rStyle" />
      <SelectedRowSty le CssClass="Selec tedRowStyle" />
      <PagerStyle CssClass="Pager RowStyle" />
      <PagerSetting s Mode="NumericFi rstLast" PageButtonCount ="5" />
      </asp:GridView>
      <asp:label runat="server" font-size="X-Small"/>

      Comment

      Working...