AspNet DataGrid won't fire sort routine

Discussion in 'ASP .Net Web Controls' started by PatLaf, Oct 29, 2003.

  1. PatLaf

    PatLaf Guest

    Hello to all,
    I have an asp.net application that uses a datagrid but it
    won't fire the sort command even though I've set the
    allow sort to true. Any idea's?

    Thanks in advance
    Patrick Laferriere
     
    PatLaf, Oct 29, 2003
    #1
    1. Advertising

  2. PatLaf

    Gary Nelson Guest

    Pat,

    I believe you have to write the sort code yourself.

    Gary

    "PatLaf" <> wrote in message
    news:0c6101c39e29$c3d32bb0$...
    > Hello to all,
    > I have an asp.net application that uses a datagrid but it
    > won't fire the sort command even though I've set the
    > allow sort to true. Any idea's?
    >
    > Thanks in advance
    > Patrick Laferriere
     
    Gary Nelson, Oct 29, 2003
    #2
    1. Advertising

  3. Hi Patrick,

    Gary is right that you need some code to make sorting work. Here is a
    sample.

    Add the following inside the HTML for the form. This adds a datagrid.

    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:
    absolute; TOP: 8px" runat="server"
    AutoGenerateColumns="False" AllowSorting="True">
    <Columns>
    <asp:BoundColumn DataField="au_id" SortExpression="au_id"
    HeaderText="au_id"></asp:BoundColumn>
    <asp:BoundColumn DataField="au_fname" SortExpression="au_fname"
    HeaderText="au_fname"></asp:BoundColumn>
    <asp:BoundColumn DataField="au_lname" SortExpression="au_lname"
    HeaderText="au_lname"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>


    Next, add the following to your code behind.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    ViewState("SortOrder") = "DESC"
    ViewState("SortField") = "au_id"
    Bind()
    End If
    End Sub

    Private Sub Bind()
    Dim connectionString As String = "server='localhost';
    trusted_connection=true; Database='pubs'"
    Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
    System.Data.SqlClient.SqlConnection(connectionString)
    Dim queryString As String = "SELECT au_id, au_fname, au_lname FROM
    Authors"
    Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
    System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    Dim sqlAdapter As New SqlClient.SqlDataAdapter(sqlCommand)
    Dim ds As New Data.DataSet
    sqlAdapter.Fill(ds, "data")
    Dim dv As DataView
    dv = ds.Tables("data").DefaultView
    dv.Sort = ViewState("SortField") & " " & ViewState("SortOrder")
    DataGrid1.DataSource = dv
    DataGrid1.DataBind()
    sqlAdapter.Dispose()
    sqlCommand.Dispose()
    sqlConnection.Close()
    sqlConnection.Dispose()
    End Sub

    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    DataGrid1.SortCommand
    If ViewState("SortField") = e.SortExpression Then
    If (ViewState("SortOrder") = "DESC") Then
    ViewState("SortOrder") = "ASC"
    Else
    ViewState("SortOrder") = "DESC"
    End If
    End If
    ViewState("SortField") = e.SortExpression
    Bind()
    End Sub

    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > From: "Gary Nelson" <>
    > References: <0c6101c39e29$c3d32bb0$>
    > Subject: Re: AspNet DataGrid won't fire sort routine
    > Date: Wed, 29 Oct 2003 17:00:22 -0000
    > Lines: 17
    > X-Priority: 3
    > X-MSMail-Priority: Normal
    > X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    > X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    > Message-ID: <>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > NNTP-Posting-Host: 102.red-80-33-63.pooles.rima-tde.net 80.33.63.102
    > Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:15715
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    > Pat,
    >
    > I believe you have to write the sort code yourself.
    >
    > Gary
    >
    > "PatLaf" <> wrote in message
    > news:0c6101c39e29$c3d32bb0$...
    > > Hello to all,
    > > I have an asp.net application that uses a datagrid but it
    > > won't fire the sort command even though I've set the
    > > allow sort to true. Any idea's?
    > >
    > > Thanks in advance
    > > Patrick Laferriere

    >
    >
    >
     
    Mike Moore [MSFT], Oct 29, 2003
    #3
  4. PatLaf

    PatLaf Guest

    I have the code posted that I'm trying to use. The only
    real difference that I see is that you set auto gen col's
    to false. Could that be a factor in why this isn't
    working? I have code for the grid and the wire up in the
    behind page. My issue is that the event never triggers.
    It fires a page.postback but never the code to handle the
    sort yet I have it there. I'm kind of confused as to why
    this won't work. If the page post's back why doesn't it
    fire the grid's sort command?

    Regards,
    Pat
    >-----Original Message-----
    >Hi Patrick,
    >
    >Gary is right that you need some code to make sorting

    work. Here is a
    >sample.
    >
    >Add the following inside the HTML for the form. This

    adds a datagrid.
    >
    ><asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT:

    8px; POSITION:
    >absolute; TOP: 8px" runat="server"
    > AutoGenerateColumns="False" AllowSorting="True">
    > <Columns>
    > <asp:BoundColumn DataField="au_id"

    SortExpression="au_id"
    >HeaderText="au_id"></asp:BoundColumn>
    > <asp:BoundColumn DataField="au_fname"

    SortExpression="au_fname"
    >HeaderText="au_fname"></asp:BoundColumn>
    > <asp:BoundColumn DataField="au_lname"

    SortExpression="au_lname"
    >HeaderText="au_lname"></asp:BoundColumn>
    > </Columns>
    ></asp:DataGrid>
    >
    >
    >Next, add the following to your code behind.
    >
    >Private Sub Page_Load(ByVal sender As System.Object,

    ByVal e As
    >System.EventArgs) Handles MyBase.Load
    > If Not IsPostBack Then
    > ViewState("SortOrder") = "DESC"
    > ViewState("SortField") = "au_id"
    > Bind()
    > End If
    >End Sub
    >
    >Private Sub Bind()
    > Dim connectionString As String = "server='localhost';
    >trusted_connection=true; Database='pubs'"
    > Dim sqlConnection As

    System.Data.SqlClient.SqlConnection = New
    >System.Data.SqlClient.SqlConnection(connectionString)
    > Dim queryString As String = "SELECT au_id, au_fname,

    au_lname FROM
    >Authors"
    > Dim sqlCommand As System.Data.SqlClient.SqlCommand =

    New
    >System.Data.SqlClient.SqlCommand(queryString,

    sqlConnection)
    > Dim sqlAdapter As New SqlClient.SqlDataAdapter

    (sqlCommand)
    > Dim ds As New Data.DataSet
    > sqlAdapter.Fill(ds, "data")
    > Dim dv As DataView
    > dv = ds.Tables("data").DefaultView
    > dv.Sort = ViewState("SortField") & " " & ViewState

    ("SortOrder")
    > DataGrid1.DataSource = dv
    > DataGrid1.DataBind()
    > sqlAdapter.Dispose()
    > sqlCommand.Dispose()
    > sqlConnection.Close()
    > sqlConnection.Dispose()
    >End Sub
    >
    >Private Sub DataGrid1_SortCommand(ByVal source As

    Object, ByVal e As
    >System.Web.UI.WebControls.DataGridSortCommandEventArgs)

    Handles
    >DataGrid1.SortCommand
    > If ViewState("SortField") = e.SortExpression Then
    > If (ViewState("SortOrder") = "DESC") Then
    > ViewState("SortOrder") = "ASC"
    > Else
    > ViewState("SortOrder") = "DESC"
    > End If
    > End If
    > ViewState("SortField") = e.SortExpression
    > Bind()
    >End Sub
    >
    >Thank you, Mike
    >Microsoft, ASP.NET Support Professional
    >
    >Microsoft highly recommends to all of our customers

    that they visit the
    >http://www.microsoft.com/protect site and perform the

    three straightforward
    >steps listed to improve your computer's security.
    >
    >This posting is provided "AS IS", with no warranties,

    and confers no rights.
    >
    >
    >--------------------
    >> From: "Gary Nelson" <>
    >> References: <0c6101c39e29$c3d32bb0$>
    >> Subject: Re: AspNet DataGrid won't fire sort routine
    >> Date: Wed, 29 Oct 2003 17:00:22 -0000
    >> Lines: 17
    >> X-Priority: 3
    >> X-MSMail-Priority: Normal
    >> X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    >> X-MimeOLE: Produced By Microsoft MimeOLE

    V6.00.2800.1165
    >> Message-ID: <>
    >> Newsgroups:

    microsoft.public.dotnet.framework.aspnet.webcontrols
    >> NNTP-Posting-Host: 102.red-80-33-63.pooles.rima-

    tde.net 80.33.63.102
    >> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!

    TK2MSFTNGP12.phx.gbl
    >> Xref: cpmsftngxa06.phx.gbl

    >microsoft.public.dotnet.framework.aspnet.webcontrols:1571

    5
    >> X-Tomcat-NG:

    microsoft.public.dotnet.framework.aspnet.webcontrols
    >>
    >> Pat,
    >>
    >> I believe you have to write the sort code yourself.
    >>
    >> Gary
    >>
    >> "PatLaf" <> wrote in message
    >> news:0c6101c39e29$c3d32bb0$...
    >> > Hello to all,
    >> > I have an asp.net application that uses a datagrid

    but it
    >> > won't fire the sort command even though I've set the
    >> > allow sort to true. Any idea's?
    >> >
    >> > Thanks in advance
    >> > Patrick Laferriere

    >>
    >>
    >>

    >
    >.
    >
     
    PatLaf, Oct 30, 2003
    #4
  5. PatLaf

    Alvin Bruney Guest

    Your sort event handler is not wired up. Verify this in the events property
    pages. It will be empty. There is a bug where it becomes unwired sometimes.

    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits
    "PatLaf" <> wrote in message
    news:093c01c39edf$9ccafc30$...
    > I have the code posted that I'm trying to use. The only
    > real difference that I see is that you set auto gen col's
    > to false. Could that be a factor in why this isn't
    > working? I have code for the grid and the wire up in the
    > behind page. My issue is that the event never triggers.
    > It fires a page.postback but never the code to handle the
    > sort yet I have it there. I'm kind of confused as to why
    > this won't work. If the page post's back why doesn't it
    > fire the grid's sort command?
    >
    > Regards,
    > Pat
    > >-----Original Message-----
    > >Hi Patrick,
    > >
    > >Gary is right that you need some code to make sorting

    > work. Here is a
    > >sample.
    > >
    > >Add the following inside the HTML for the form. This

    > adds a datagrid.
    > >
    > ><asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT:

    > 8px; POSITION:
    > >absolute; TOP: 8px" runat="server"
    > > AutoGenerateColumns="False" AllowSorting="True">
    > > <Columns>
    > > <asp:BoundColumn DataField="au_id"

    > SortExpression="au_id"
    > >HeaderText="au_id"></asp:BoundColumn>
    > > <asp:BoundColumn DataField="au_fname"

    > SortExpression="au_fname"
    > >HeaderText="au_fname"></asp:BoundColumn>
    > > <asp:BoundColumn DataField="au_lname"

    > SortExpression="au_lname"
    > >HeaderText="au_lname"></asp:BoundColumn>
    > > </Columns>
    > ></asp:DataGrid>
    > >
    > >
    > >Next, add the following to your code behind.
    > >
    > >Private Sub Page_Load(ByVal sender As System.Object,

    > ByVal e As
    > >System.EventArgs) Handles MyBase.Load
    > > If Not IsPostBack Then
    > > ViewState("SortOrder") = "DESC"
    > > ViewState("SortField") = "au_id"
    > > Bind()
    > > End If
    > >End Sub
    > >
    > >Private Sub Bind()
    > > Dim connectionString As String = "server='localhost';
    > >trusted_connection=true; Database='pubs'"
    > > Dim sqlConnection As

    > System.Data.SqlClient.SqlConnection = New
    > >System.Data.SqlClient.SqlConnection(connectionString)
    > > Dim queryString As String = "SELECT au_id, au_fname,

    > au_lname FROM
    > >Authors"
    > > Dim sqlCommand As System.Data.SqlClient.SqlCommand =

    > New
    > >System.Data.SqlClient.SqlCommand(queryString,

    > sqlConnection)
    > > Dim sqlAdapter As New SqlClient.SqlDataAdapter

    > (sqlCommand)
    > > Dim ds As New Data.DataSet
    > > sqlAdapter.Fill(ds, "data")
    > > Dim dv As DataView
    > > dv = ds.Tables("data").DefaultView
    > > dv.Sort = ViewState("SortField") & " " & ViewState

    > ("SortOrder")
    > > DataGrid1.DataSource = dv
    > > DataGrid1.DataBind()
    > > sqlAdapter.Dispose()
    > > sqlCommand.Dispose()
    > > sqlConnection.Close()
    > > sqlConnection.Dispose()
    > >End Sub
    > >
    > >Private Sub DataGrid1_SortCommand(ByVal source As

    > Object, ByVal e As
    > >System.Web.UI.WebControls.DataGridSortCommandEventArgs)

    > Handles
    > >DataGrid1.SortCommand
    > > If ViewState("SortField") = e.SortExpression Then
    > > If (ViewState("SortOrder") = "DESC") Then
    > > ViewState("SortOrder") = "ASC"
    > > Else
    > > ViewState("SortOrder") = "DESC"
    > > End If
    > > End If
    > > ViewState("SortField") = e.SortExpression
    > > Bind()
    > >End Sub
    > >
    > >Thank you, Mike
    > >Microsoft, ASP.NET Support Professional
    > >
    > >Microsoft highly recommends to all of our customers

    > that they visit the
    > >http://www.microsoft.com/protect site and perform the

    > three straightforward
    > >steps listed to improve your computer's security.
    > >
    > >This posting is provided "AS IS", with no warranties,

    > and confers no rights.
    > >
    > >
    > >--------------------
    > >> From: "Gary Nelson" <>
    > >> References: <0c6101c39e29$c3d32bb0$>
    > >> Subject: Re: AspNet DataGrid won't fire sort routine
    > >> Date: Wed, 29 Oct 2003 17:00:22 -0000
    > >> Lines: 17
    > >> X-Priority: 3
    > >> X-MSMail-Priority: Normal
    > >> X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    > >> X-MimeOLE: Produced By Microsoft MimeOLE

    > V6.00.2800.1165
    > >> Message-ID: <>
    > >> Newsgroups:

    > microsoft.public.dotnet.framework.aspnet.webcontrols
    > >> NNTP-Posting-Host: 102.red-80-33-63.pooles.rima-

    > tde.net 80.33.63.102
    > >> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!

    > TK2MSFTNGP12.phx.gbl
    > >> Xref: cpmsftngxa06.phx.gbl

    > >microsoft.public.dotnet.framework.aspnet.webcontrols:1571

    > 5
    > >> X-Tomcat-NG:

    > microsoft.public.dotnet.framework.aspnet.webcontrols
    > >>
    > >> Pat,
    > >>
    > >> I believe you have to write the sort code yourself.
    > >>
    > >> Gary
    > >>
    > >> "PatLaf" <> wrote in message
    > >> news:0c6101c39e29$c3d32bb0$...
    > >> > Hello to all,
    > >> > I have an asp.net application that uses a datagrid

    > but it
    > >> > won't fire the sort command even though I've set the
    > >> > allow sort to true. Any idea's?
    > >> >
    > >> > Thanks in advance
    > >> > Patrick Laferriere
    > >>
    > >>
    > >>

    > >
    > >.
    > >
     
    Alvin Bruney, Oct 30, 2003
    #5
    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. Chad
    Replies:
    4
    Views:
    8,339
  2. =?Utf-8?B?TWFyZHk=?=

    Javascript from Datagrid won't fire

    =?Utf-8?B?TWFyZHk=?=, Nov 21, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    447
    =?Utf-8?B?TWFyZHk=?=
    Nov 25, 2005
  3. rkk
    Replies:
    9
    Views:
    813
    CBFalconer
    Sep 24, 2006
  4. KJ
    Replies:
    2
    Views:
    184
    Stevie_mac
    Aug 16, 2003
  5. Navin
    Replies:
    1
    Views:
    700
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page