Extend functionality of datagrid12.aspx sample

Discussion in 'ASP .Net Datagrid Control' started by Sal Young, Dec 5, 2003.

  1. Sal Young

    Sal Young Guest

    I want to add "Paging" to a DataGrid that already has sort by column
    funtionality. Here I'm including the code I have so far which displays a
    two column DataGrid with letters and numbers. The letters are stored in
    ascending order (a, b, c, d, e, etc.) while numbers are in descending order
    (26, 25, 24, 23, etc.) in the database table. In other words, a = 26, b =
    25, c = 24.



    -----------------------------------------------------------
    sub Page_Load(Source as Object, e as EventArgs)
    if not isPostback then
    bindDataGrid("letters")
    end if
    end sub

    sub sortMyDataGrid(a as object, e as DataGridSortCommandEventArgs)
    bindDataGrid(e.SortExpression)
    end sub

    sub bindDataGrid(SortField as string)
    Dim myConn as sqlConnection
    Dim connStr as String
    Dim DS as DataSet
    Dim myCommand as SqlDataAdapter

    connStr = "server=myservername;database=test;uid=sa;pwd=SoFoHaI"
    myConn = new sqlConnection(connStr)
    myCommand = new SqlDataAdapter("SELECT letters, numbers FROM tbl_sort",
    myConn)

    DS = new DataSet()
    MyCommand.Fill(DS, "tbl_sort")
    Dim Source as DataView = DS.Tables("tbl_sort").DefaultView
    Source.Sort = SortField
    DataGrid1.DataSource = Source
    DataGrid1.DataBind()
    myConn.Close()
    end sub

    'sub doPaging(a as object, e as DataGridPageChangedEventArgs)
    ' dataGrid1.CurrentPageIndex=e.NewPageIndex
    ' bindDataGrid
    'end sub
    -----------------------------------------------------------

    <asp:datagrid id="dataGrid1"
    Runat="server"
    DataKeyField="letters"
    onSortCommand="sortMyDataGrid"
    AllowSorting="True"
    autoGenerateColumns="false">
    <Columns>
    <asp:BoundColumn
    DataField="letters"
    HeaderText="Letter"
    SortExpression="letters">
    </asp:BoundColumn>
    <asp:BoundColumn
    DataField="numbers"
    HeaderText="Numbersl"
    SortExpression="numbers">
    </asp:BoundColumn>
    </Columns>
    </asp:datagrid>
    Sal Young, Dec 5, 2003
    #1
    1. Advertising

  2. Sal Young

    Sal Young Guest

    I found an article at
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
    /aspnet-pageablesortable.asp "Creating a Pageable, Sortable DataGrid"

    Thanks.


    "Sal Young" <> wrote in message
    news:...
    > I want to add "Paging" to a DataGrid that already has sort by column
    > funtionality. Here I'm including the code I have so far which displays a
    > two column DataGrid with letters and numbers. The letters are stored in
    > ascending order (a, b, c, d, e, etc.) while numbers are in descending

    order
    > (26, 25, 24, 23, etc.) in the database table. In other words, a = 26, b =
    > 25, c = 24.
    >
    >
    >
    > -----------------------------------------------------------
    > sub Page_Load(Source as Object, e as EventArgs)
    > if not isPostback then
    > bindDataGrid("letters")
    > end if
    > end sub
    >
    > sub sortMyDataGrid(a as object, e as DataGridSortCommandEventArgs)
    > bindDataGrid(e.SortExpression)
    > end sub
    >
    > sub bindDataGrid(SortField as string)
    > Dim myConn as sqlConnection
    > Dim connStr as String
    > Dim DS as DataSet
    > Dim myCommand as SqlDataAdapter
    >
    > connStr = "server=myservername;database=test;uid=sa;pwd=SoFoHaI"
    > myConn = new sqlConnection(connStr)
    > myCommand = new SqlDataAdapter("SELECT letters, numbers FROM

    tbl_sort",
    > myConn)
    >
    > DS = new DataSet()
    > MyCommand.Fill(DS, "tbl_sort")
    > Dim Source as DataView = DS.Tables("tbl_sort").DefaultView
    > Source.Sort = SortField
    > DataGrid1.DataSource = Source
    > DataGrid1.DataBind()
    > myConn.Close()
    > end sub
    >
    > 'sub doPaging(a as object, e as DataGridPageChangedEventArgs)
    > ' dataGrid1.CurrentPageIndex=e.NewPageIndex
    > ' bindDataGrid
    > 'end sub
    > -----------------------------------------------------------
    >
    > <asp:datagrid id="dataGrid1"
    > Runat="server"
    > DataKeyField="letters"
    > onSortCommand="sortMyDataGrid"
    > AllowSorting="True"
    > autoGenerateColumns="false">
    > <Columns>
    > <asp:BoundColumn
    > DataField="letters"
    > HeaderText="Letter"
    > SortExpression="letters">
    > </asp:BoundColumn>
    > <asp:BoundColumn
    > DataField="numbers"
    > HeaderText="Numbersl"
    > SortExpression="numbers">
    > </asp:BoundColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >
    Sal Young, Dec 9, 2003
    #2
    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. Jake
    Replies:
    0
    Views:
    526
  2. Tomás Ó hÉilidhe

    Extend functionality of printf (Add colours)

    Tomás Ó hÉilidhe, Nov 10, 2008, in forum: C Programming
    Replies:
    16
    Views:
    577
    Tomás Ó hÉilidhe
    Nov 11, 2008
  3. Robert Klemme
    Replies:
    8
    Views:
    138
    Robert Dober
    Sep 7, 2008
  4. jacster
    Replies:
    4
    Views:
    73
    jacster
    Jun 5, 2005
  5. Tim Chase
    Replies:
    0
    Views:
    58
    Tim Chase
    Feb 16, 2014
Loading...

Share This Page