We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

[4686] Dynamic Column Sorting and Paging in ASP.NET

Last post 08-09-2006 3:44 PM by dcperry. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4686] Dynamic Column Sorting and Paging in ASP.NET

    This thread is for discussions of Dynamic Column Sorting and Paging in ASP.NET.

    • Post Points: 30
  • 05-03-2005 10:51 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45

    Dynamic Column Sorting and Paging Code in C#

    Hi all,

    For all you C# coders out there, I've included the article's code in C# to save anyone the headache of converting it.

    Code:

    <%@ Page Language="C#" Debug="False" Strict="True" Explicit="True" Buffer="True"%>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <html>
    <head>
    <title>Dynamic Column Sorting and Paging in ASP.NET</title>
    </head>
    <body bgcolor="#FFFFFF" topmargin="0" marginheight="0">

    <script language="C#" runat="server">
    void Page_Load (Object sender, EventArgs e) {

       if(SortExp.Text == ""){
           BindSQL ("title asc");
       } else {
           BindSQL (SortExp.Text);
       }

    }

    void BindSQL (string SortField) {
       SqlConnection MyConnection;
       SqlDataAdapter MyCommand;
       string sqlStr = "SELECT authors.au_fname, authors.au_lname, titles.title, titles.price FROM authors, titleauthor, titles WHERE titleauthor.au_id = authors.au_id AND titleauthor.title_id = titles.title_id ORDER BY title asc";
       string strConn = "server=(local);uid=sa;pwd=;database=Pubs;";
       int RcdCount;

       SortExp.Text = SortField;

       MyConnection = new SqlConnection(strConn);
       MyCommand = new SqlDataAdapter(sqlStr, MyConnection);
       DataSet DS = new DataSet();
       MyCommand.Fill(DS, "Pubs");
       DataView Source = DS.Tables[0].DefaultView;
       Source.Sort = SortField;
       Pubs.DataSource = Source;
       Pubs.DataBind();
    }

    string SortOrder (string Field) {
       string so = SortExp.Text;
       string newString;

       if (Field == so) {
           newString = Field.Replace ("asc","desc");
       }else {
           newString = Field.Replace ("desc","asc");
       }
       return newString;
    }

    public void BookList_Sort (Object Sender, DataGridSortCommandEventArgs E) {
       Pubs.CurrentPageIndex = 0; //To sort from top
       BindSQL (SortOrder (E.SortExpression).ToString()); //Rebind our Datagrid
    }

    public void BookList_PageChange (Object Source, DataGridPageChangedEventArgs E) {
       Pubs.CurrentPageIndex = E.NewPageIndex;
       BindSQL(SortExp.Text);
    }
    </script>
    <BR><BR>
    <H2>Dynamic Column Sorting and Paging in ASP.NET</H2>
    <BR><BR>
    <form runat="server">
       <ASP:Datagrid id="Pubs" runat="server"
       Pagesize="10"
       AllowSorting="True"
       AllowPaging="True"
       AllowCustomPaging="False"
       PagerStyle-Visible = "True"
       PagerStyle-Mode = "NumericPages"
       HeaderStyle-BackColor="Blue"
       HeaderStyle-ForeColor="White"
       OnSortCommand="BookList_Sort"
       OnPageIndexChanged="BookList_PageChange"
       AutoGenerateColumns="false"
       >
       <Columns>
           <asp:BoundColumn HeaderText="Title" SortExpression="title asc" DataField="title" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
           <asp:BoundColumn HeaderText="Last Name" SortExpression="au_lname asc" DataField="au_lname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
           <asp:BoundColumn HeaderText="First Name" SortExpression="au_fname asc" DataField="au_fname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
           <asp:BoundColumn HeaderText="Price" SortExpression="price asc" DataField="price" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
       </Columns>
       </ASP:DataGrid>
       <asp:Label id="SortExp" runat="server" Visible="False" />
    </form>
    </body>
    </html>


    - Jimmy Markatos
    • Post Points: 0
  • 06-10-2005 9:38 AM In reply to

    • acurra79
    • Not Ranked
    • Joined on 06-10-2005
    • New Member
    • Points 5
    Hi,

    I notice the following error when I try to run the code:

    When you navigate to the second, third, etc page (order by ASC only), the sorting order is incorrect.
    This is because the SortOrder function change the direction from asc to desc.

    To correct this error, change the the line:

    BindSQL(SortOrder(SortExp.Text));

    TO

    BindSQL(SortExp.Text);
    • Post Points: 0
  • 06-10-2005 5:34 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45
    Hey acurra79,

    Nice catch! Clearly an oversight. Thanks. I have corrected the C# code and the article as well.

    - Jimmy Markatos
    • Post Points: 0
  • 06-10-2005 5:47 PM In reply to

    how to make installable file of the project

    hi
    i have to submit my project on 20th of this month i have made it but the thing which is left is that i want to make a setup file of the whole project like the one of MSN messenger... i think to might have understood what i wana say...

    i want to deliever the project setup file... can u help me do reple on ahmed_muneeb@hotmail.com

    in the setup file i want that the connectivity is done automatically and i dont have to bring my PC along...

    i have used VB 6.0 and access...... for database....

    thanks
    • Post Points: 0
  • 06-13-2005 12:57 PM In reply to

    Hi Ahmed,
      Click Startmenu -> program - > Microsoft Visual Studio 6.0 -> Microsoft Visual Studio 6.0 Tools -> Package & Deployment Wizard. Browse to locate your project for which setup is to be created and click on Package icon. This will take you to "package and deployment wizard - Package type". Select the package type as "Standard Setup package" and click on next button and rest are self explanatory.

    Thanks

    • Post Points: 0
  • 08-09-2006 3:44 PM In reply to

    • dcperry
    • Not Ranked
    • Joined on 08-09-2006
    • New Member
    • Points 5

    Re: [4686] Dynamic Column Sorting and Paging in ASP.NET

    I usually don't post comments but in this case I feel it's warranted. This was a very useful article. I attempted to use the MSDN example but I usually end up with a headache when I do. Thank-you for posting this information. Code works very well.
    • Post Points: 5
Page 1 of 1 (7 items)