How to bring paging, sorting and hyperlinking functioanlities in a datagrid?

Discussion in 'ASP .Net Datagrid Control' started by Crazy Learner, Jul 22, 2003.

  1. Hi there,
    I just started asp.net code 3 days ago from now.
    I have a situation in one of my pages that i need to provide all three
    functionailities paging, sorting all of the columns and hyperlinking of
    the two columns in the datagrid. I am very glad that
    i could page and sort and enjoyed the result.
    inorder to hyperlink couple of columns in the
    datagrid i made AutoGenerateColumns="False"
    (if i don't set this value "False" the datagrid displaying duplicate
    columns) and used
    <asp:hyperlinkcolumn> to the column i want
    hyperlink and <asp:boundcolumn> for the rest of the
    columns to display them asusual in the datagrid. I
    am able to hyperlink the mentioned columns in the
    order i wanted to and also able to page the results now.
    But unfortunately i lost sorting functionality. Now i
    am only able to page and hyperlink but not sorting.
    Hope you got where i am strucking at. Please help me
    to get out of this problem.thanks much and appreciate
    your encouragement for the new programers :) thanks
    once again. I am pasting my Html and code behind pages
    underneath. i am just using customers table in
    northwind database in sqlserver 2000.
    *************** Html part of the.aspx***************

    <%@ Register TagPrefix="mbrsc"
    Namespace="MetaBuilders.WebControls"
    Assembly="MetaBuilders.WebControls.RowSelectorColumn"
    %>
    <%@ Page Language="vb" Src="project_listing.aspx.vb"
    Inherits="project_listing" %>
    <html>
    <head>
    <title>Paging and Sorting in a DataGrid</title>
    <meta name="GENERATOR" content="Microsoft Visual
    Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic
    7.0">
    <meta name=vs_defaultClientScript
    content="JavaScript">
    <meta name=vs_targetSchema

    content="http://schemas.microsoft.com/intellisense/ie5"
    </head>
    <body>
    <h3 align=center><font face="Verdana">Project
    Listing</font></h3>
    <form runat="server">

    <table>
    <tr><td>
    <asp:radiobutton id="Radiobutton1" runat="server"
    groupname="projectselection"></asp:radiobutton>My
    </td>
    <td>Projects
    <asp:radiobutton id="Radiobutton2" runat="server"
    groupname="projectselection"></asp:radiobutton>Shared
    </td>
    <td>Projects
    <asp:radiobutton id="Radiobutton3" runat="server"
    groupname="projectselection"
    checked="true"></asp:radiobutton>All</td>
    </table>
    <p></p>
    <P></P>
    <asp:Label id="lblOrderBy" runat="server"
    Visible="False" />
    <asp:DataGrid id="dtgCusts"
    align="center"
    AllowPaging="True"

    OnPageIndexChanged="PageIndexChanged_Click"
    PageSize="10"
    PagerStyle-Mode="NumericPages"
    PagerStyle-Position="Top"
    PagerStyle-HorizontalAlign="Center"
    PagerStyle-CssClass="pageLinks"
    AutoGenerateColumns="False"
    AllowSorting="True"
    OnSortCommand="SortCommand_Click"

    runat="server">
    <Columns>
    <mbrsc:RowSelectorColumn selectionMode="Single"
    />
    <asp:boundcolumn headertext="Company Name"
    datafield="CompanyName" />
    <asp:HyperLinkColumn DataTextField="Contactname"
    HeaderText="Contact Name"
    DataNavigateUrlField="contactname"

    datanavigateurlformatstring="mainIndex.aspx?Id={0}>"
    />
    <asp:boundcolumn headertext="ContactTitle"
    datafield="ContactTitle" />
    </Columns>
    </asp:datagrid>

    <p></p>
    <p></p>


    <table align="center" border="0">
    <tr>
    <td><asp:button id="tbnNew" text="New"
    runat="server" /></td>
    <td> </td>
    <td><asp:button id="btnAssign" text="Assign"
    runat="server" /></td>
    <td> </td>
    <td><asp:button id="btnDelete" text="Delete"
    runat="server" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    ***************End of the html part .aspx

    ***** code behind code part of the
    .aspx.vb**********
    Imports System
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration

    Public Class project_listing : Inherits Page

    Protected dtgCusts As DataGrid
    Protected lblOrderBy As Label
    Protected strSql As String = "SELECT
    CompanyName,
    ContactName, ContactTitle, Phone FROM Customers"

    Sub Page_Load(ByVal sender As Object, ByVal e As
    EventArgs)
    If Not Page.IsPostBack Then
    BindTheData()
    End If
    End Sub

    Sub BindTheData()
    'Dim strConn As String
    'strConn =

    ConfigurationSettings.AppSettings"NorthwindConnection")

    Dim strConn As String =
    "server=movva;database=NorthWind;user
    id=sa;password=pradev"
    strSql = strSql + lblOrderBy.Text
    Dim myDataSet As DataSet = New DataSet()
    Dim myDataAdapter As SqlDataAdapter = New
    SqlDataAdapter(strSql, strConn)
    myDataAdapter.Fill(myDataSet, "Customers")

    dtgCusts.DataSource =
    myDataSet.Tables("Customers")

    dtgCusts.DataBind()
    End Sub

    Sub PageIndexChanged_Click(ByVal sender As
    Object,
    ByVal e As DataGridPageChangedEventArgs)
    dtgCusts.CurrentPageIndex = e.NewPageIndex
    BindTheData()
    End Sub

    Sub SortCommand_Click(ByVal sender As Object,
    ByVal e As DataGridSortCommandEventArgs)
    lblOrderBy.Text = " ORDER BY " &
    e.SortExpression
    BindTheData()
    End Sub

    End Class

    ****************** end of the codebehind coe**********
    Crazy Learner, Jul 22, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    5
    Views:
    4,787
    mkr04
    Sep 7, 2006
  2. Gmansky

    hyperlinking to a new page

    Gmansky, Jun 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    543
    EijiTek
    Jun 27, 2004
  3. Nedu N

    Hyperlinking and ASP.NEt Session

    Nedu N, Dec 7, 2003, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    140
    Munsifali Rashid
    Dec 9, 2003
  4. Nedu N

    Hyperlinking and ASP.NEt Session

    Nedu N, Dec 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    174
    Munsifali Rashid
    Dec 9, 2003
  5. Ruby Baby
    Replies:
    3
    Views:
    106
    Minero Aoki
    Feb 15, 2004
Loading...

Share This Page