HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

Rate It (26)

Last post 07-30-2007 2:56 AM by krishanmanral. 185 replies.

Sort Posts:

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    01-31-2007, 7:31 PM
    Locked
    • Member
      32 point Member
    • Erwin@ODS
    • Member since 01-24-2007, 1:17 PM
    • Ghent - Belgium
    • Posts 14

    Can I also put my 2 cents here ?

    I posted an article on how to get a GridView do automatic paging programmatically (i.e. at run time) au lieu of declaratively (at design time).

    However this method uses a ObjectDataSource (but NOT in design time).

    You'll find the article here : http://forums.asp.net/thread/1554769.aspx.

    Please feel free to comment !

    Erwin

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-01-2007, 1:43 PM
    Locked
    • Member
      165 point Member
    • jpa7227
    • Member since 11-09-2006, 2:33 PM
    • Rochester, NY
    • Posts 58

    First, thanks for the code to get started here, Ryan.  It's helped greatly.  I'm having some issues with the Paging, though.

    My GridView is populated via a DataSet (actually, I set the DataSource to the first table in the DataSet).  I grab this DataSet via Remoting.  My problem is this, the GridView will be re-bound to a new DataSet based on some search criteria on the same page.  So, what I did was create a private global DataSet.  When I grab a new DataSet from the server, I assign it to the global object then re-bind that to the GridView's DataSource.  I've pretty much used all of the code you have in the Strong Coders link (sans the populate method, of course).  The one thing I do differently is substitute the global DataSet.Tables(0) in for the GridView's DataSource in the SortDataTable function.

    The problem is, when I click on the link for another page in the GridView, I lose the paging footer.  It moves to the next page, but the paging information is gone.  Any ideas here?

     

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-07-2007, 6:19 AM
    Locked
    • Member
      9 point Member
    • dipu
    • Member since 12-12-2006, 4:11 PM
    • Posts 26

    Hi StrongTypes,

    I've come across this post as I am trying to sort my grid view. It's been a year since you've made the post so I hope you can still help!

    I have a data table which populates a datagrid. I understand that I need to create a dataview. I am trying to sort one of the columns in DESCENDING order. Here is my code so far:

    1    Dim dt As New Data.DataTable
    2    dt = ViewState("GV1")        
    3    Dim dataView As New Data.DataView(dt)
    4    
    5    Dim s As SortDirection
    6    s = SortDirection.Descending
    7    dataView.Sort = String.Format("{0} {1}", e.SortExpression, s)
    8    
    9    GV1.DataSource = dt
    10   GV1.DataBind()
    

     On line 7 I get an error telling me that "there is no column named price descending", where price is the column I am trying to sort in descending order.

    Can you help?

    Thanks

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-14-2007, 2:37 PM
    Locked
    • Member
      81 point Member
    • quest3297
    • Member since 08-03-2006, 9:37 PM
    • Posts 68
    hi i tried to use your code but unable to the gridview datasource it always returns me the null is there anyway to get the datasource of the grid.I cant keep the dataset in the session as it contains 1000s of records.Thank you.
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    03-09-2007, 8:37 AM
    Locked
    • Member
      117 point Member
    • MarcoLF
    • Member since 09-16-2006, 10:35 AM
    • Posts 44

    Hi I am using a SqlDataReader as dataSource.

    Is it possible to adapt the code?

    If I try to use the code on the first page I always get dataTable=null.

    must i use a datatable necessarily? 

    Thank you for help. 

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    03-12-2007, 11:41 AM
    Locked
    • Member
      2 point Member
    • fartboy
    • Member since 03-12-2007, 3:31 PM
    • Posts 2

    Hi ,

    Not sure if this is the right thread for this but here we go

     I am trying to use the PageDataSource class and i do not wish to bind to a control, I want to be able to acees the data manually

    i have searched far and wide and came up with this code

     

    objPage.DataSource = TTable.DefaultView

     

    objPage.AllowPaging =

    True

    objPage.PageSize = 1

    objPage.CurrentPageIndex = 4

     

    Dim enumerator1 As IEnumerator = Nothing

    Dim dt As DataTable

    Dim table2 As DataTable = DirectCast(objPage.DataSource, DataView).Table

    dt = table2.Clone

    enumerator1 = objPage.GetEnumerator

    While enumerator1.MoveNext()

    Dim obj As Object = enumerator1.Current

    Dim drv As Data.DataRowView = DirectCast(obj, Data.DataRowView)

    dt.ImportRow(drv.Row)

    End While

     This code works but always returns the first row of data, and as can be seen i wish to get the 4th item,

    Can anyone show me the error of my ways...??

     

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    03-14-2007, 12:21 PM
    Locked
    • Member
      2 point Member
    • smhouston
    • Member since 01-03-2007, 2:25 PM
    • Sheffield,UK
    • Posts 8
    I've had to do it the way ChrisOngsuco said by using a custom method first. It works fine now though. Maybe it's because I only databind my gridview when the page is first loaded (when it's not postback)
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    04-26-2007, 8:36 AM
    Locked
    • Member
      2 point Member
    • infodemers
    • Member since 04-23-2007, 4:18 PM
    • Montreal, Canada
    • Posts 17

    Hello Ryan,

    I am using your VB.net code for sorting and paging my gridview and I can't get it done. It displays the first 25 items but when I click on sorting or paging, I get  my "EmptyDataText" No Record Found message. I know there is more data to display. My data source comes from a SQL server. I don't know what else I should specify for the moment. Here is my complete code.

    I thank you very much in advance for any help!

    <%

    @ Page Language="VB" %>

    <%

    @ Import Namespace="System.Data" %>

    <%

    @ Import Namespace="System.Data.SqlClient" %>

    <%

    @ Import Namespace="System.Web.UI.WebControls" %>

    <%

    @ PreviousPageType VirtualPath="~/Default.aspx" %>

    <

    script runat="server">

    Private Sub PopulateGridView1()

    If Not Page.PreviousPage Is Nothing Then

    Dim pp_DDL_Aclli As DropDownList

    Dim pp_DDL_Region As DropDownList

    Dim pp_DDL_Subregion As DropDownList

    Dim pp_DDL_Zclli As DropDownList

    Dim pp_DDL_Pec_code As DropDownList

    Dim pp_TB_Date_In As TextBox

    Dim pp_TB_Date_Out As TextBox

    Dim pp_TB_Zclli As TextBox

    Dim pp_TB_Mod_Number As TextBox

    Dim pp_TB_Port_Number As TextBox

    Dim pp_LB_Status As ListBox

    Dim pp_RB_DTC As RadioButton

    Dim pp_RB_SPM As RadioButton

    Dim pp_RB_DTCI As RadioButton

    Dim pp_RB_GWC As RadioButton

    Dim pp_CB_All As CheckBox

    Dim pp_CB_Pec_Code As CheckBox

    pp_DDL_Aclli =

    CType(PreviousPage.FindControl("DDL_Aclli"), DropDownList)

    pp_DDL_Region =

    CType(PreviousPage.FindControl("DDL_Region"), DropDownList)

    pp_DDL_Subregion =

    CType(PreviousPage.FindControl("DDL_Subregion"), DropDownList)

    pp_DDL_Zclli =

    CType(PreviousPage.FindControl("DDL_Zclli"), DropDownList)

    pp_DDL_Pec_code =

    CType(PreviousPage.FindControl("DDL_Pec_code"), DropDownList)

    pp_TB_Date_In =

    CType(PreviousPage.FindControl("TB_Date_In"), TextBox)

    pp_TB_Date_Out =

    CType(PreviousPage.FindControl("TB_Date_Out"), TextBox)

    pp_TB_Zclli =

    CType(PreviousPage.FindControl("TB_Zclli"), TextBox)

    pp_TB_Mod_Number =

    CType(PreviousPage.FindControl("TB_Mod_Number"), TextBox)

    pp_TB_Port_Number =

    CType(PreviousPage.FindControl("TB_Port_Number"), TextBox)

    pp_LB_Status =

    CType(PreviousPage.FindControl("LB_Status"), ListBox)

    pp_RB_DTC =

    CType(PreviousPage.FindControl("RB_DTC"), RadioButton)

    pp_RB_SPM =

    CType(PreviousPage.FindControl("RB_SPM"), RadioButton)

    pp_RB_DTCI =

    CType(PreviousPage.FindControl("RB_DTCI"), RadioButton)

    pp_RB_GWC =

    CType(PreviousPage.FindControl("RB_GWC"), RadioButton)

    pp_CB_All =

    CType(PreviousPage.FindControl("CB_All"), CheckBox)

    pp_CB_Pec_Code =

    CType(PreviousPage.FindControl("CB_Pec_Code"), CheckBox)

     

    'Get the Value of ListBox status.

    Dim Status As String = pp_LB_Status.SelectedValue

     

    If (Status) = "ALL" Then

    Status =

    "%"

    End If

     

    'Get the Z_CLLI value from the TextBox Z_clli or from the DropDownList Z_clli.

    Dim Zclli As String = pp_DDL_Zclli.SelectedItem.ToString

    Dim TB_Zclli As String = pp_TB_Zclli.Text

     

    If (pp_CB_All.Checked) = True Then

    Zclli =

    "%"

    ElseIf Not (TB_Zclli) = "" Then

    Zclli = pp_TB_Zclli.Text

    End If

     

    'Get the Pec_Code value.

    Dim CD_PEC As String = pp_DDL_Pec_code.SelectedItem.ToString

     

    If (pp_CB_Pec_Code.Checked) = True Then

    CD_PEC =

    "%"

    End If

     

    'Get the MOD type from the Radio Buttons.

    Dim Mod_DTC As String

    Dim Mod_SPM As String

    Dim Mod_DTCI As String

    Dim Mod_GWC As String

     

    If pp_RB_DTC.Checked = True Then

    Mod_DTC =

    "DTC"

    Else

    Mod_DTC =

    "Null"

    End If

     

    If pp_RB_SPM.Checked = True Then

    Mod_SPM =

    "SPM"

    Else

    Mod_SPM =

    "Null"

    End If

     

    If pp_RB_DTCI.Checked = True Then

    Mod_DTCI =

    "DTCI"

    Else

    Mod_DTCI =

    "Null"

    End If

     

    If pp_RB_GWC.Checked = True Then

    Mod_GWC =

    "GWC"

    Else

    Mod_GWC =

    "Null"

    End If

     

    'Get The value from Date_In.

    Dim Date_In As String = pp_TB_Date_In.Text

     

    If (Date_In) = "" Then

    Date_In =

    "%"

    End If

     

    'Get The value from Date_Out

    Dim Date_Out As String = pp_TB_Date_Out.Text

     

    If (Date_Out) = "" Then

    Date_Out =

    "%"

    End If

     

    'Get the Value from the Module Number Texbox.

    Dim Mod_Number As String = pp_TB_Mod_Number.Text

     

    If (Mod_Number) = "" Then

    Mod_Number =

    "%"

    End If

     

    'Get the Value from the PI Texbox.

    Dim Port_No As String = pp_TB_Port_Number.Text

     

    If (Port_No) = "" Then

    Port_No =

    "%"

    End If

     

    ' Dim DDL_Region As String = pp_DDL_Region.SelectedItem.ToString

    ' Dim DDL_Subregion As String = pp_DDL_Subregion.SelectedItem.ToString

     

    'Built the query

    Dim DDL_Aclli As String = pp_DDL_Aclli.SelectedItem.ToString

    Dim queryString As String = "Select [tbl_Asset].* From [tbl_Asset] Where (([tbl_Asset].[A] = '" + DDL_Aclli + "') and" & _

    "([tbl_Asset].[Z] like '" + Zclli + "') and ([tbl_Asset].[Status] like '" + Status + "') And (([tbl_Asset].[MOD] like " & _

    "'" + Mod_DTC + "') or ([tbl_Asset].[MOD] like '" + Mod_SPM + "') or ([tbl_Asset].[MOD] like '" + Mod_DTCI + "') or ([tbl_Asset]." & _

    "[MOD] like '" + Mod_GWC + "')) and ([tbl_Asset].[IN_DATE] like '" + Date_In + "') and ([tbl_Asset].[OUT_DATE] like '" + Date_Out + "" & _

    "') and ([tbl_Asset].[CD_PEC] like '" + CD_PEC + "') and ([tbl_Asset].[MOD_NO] like '" + Mod_Number + "') and ([tbl_Asset].[PI] like '" + Port_No + "'));"

     

    'Run the query and bind the resulting DataTable to the GridView control.

    Dim dt As DataTable = GetData(queryString)

    Dim dataTableRowCount As Integer = dt.Rows.Count

    If dataTableRowCount > 0 Then

    GridView1.DataSource = dt

    GridView1.DataBind()

    Else

    Lable1.Text =

    "There is no data for this query."

    End If

    End If

    End Sub

     

    Function GetData(ByVal queryString As String) As DataTable

    'Retrieve the connection string stored in the web.config.file.

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("T_SwitchDBConnectionString").ConnectionString

    Dim dt As New DataTable()

    Try

    'Connect to the database and run the query.

    Dim connection As New SqlConnection(connectionString)

    Dim adapter As New SqlDataAdapter(queryString, connection)

    'Fill the DataTable

    adapter.Fill(dt)

    Catch ex As Exception

    'The connection failed. Dislpay an error message.

    Lable1.Text =

    "Unable to connect to the database."

    End Try

    Return dt

    End Function

     

    'The following lines code are to allow Paging And Sorting.

    Private Property GridViewSortDirection() As String

    Get

    Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))

    End Get

    Set(ByVal value As String)

    ViewState(

    "SortDirection") = value

    End Set

    End Property

     

    Private Property GridViewSortBLOCKED EXPRESSION As String

    Get

    Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))

    End Get

    Set(ByVal value As String)

    ViewState(

    "SortExpression") = value

    End Set

    End Property

    Private Function GetSortDirection() As String

    Select Case GridViewSortDirection

    Case "ASC"

    GridViewSortDirection =

    "DESC"

    Case "DESC"

    GridViewSortDirection =

    "ASC"

    End Select

    Return GridViewSortDirection

    End Function

     

    Protected Sub gridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

    GridView1.DataSource = SortDataTable(GridView1.DataSource,

    True)

    GridView1.PageIndex = e.NewPageIndex

    GridView1.DataBind()

    End Sub

     

    Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView

    If Not dataTable Is Nothing Then

    Dim dataView As New DataView(dataTable)

    If GridViewSortExpression <> String.Empty Then

    If isPageIndexChanging Then

    dataView.Sort =

    String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)

    Else

    dataView.Sort =

    String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())

    End If

    End If

    Return dataView

    Else

    Return New DataView()

    End If

    End Function

     

    Protected Sub gridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

    GridViewSortExpression = e.SortExpression

    Dim pageIndex As Integer = GridView1.PageIndex

    GridView1.DataSource = SortDataTable(GridView1.DataSource,

    False)

    GridView1.DataBind()

    GridView1.PageIndex = pageIndex

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    PopulateGridView1()

    End Sub

     

    </

    script>

    <!

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <

    html xmlns="http://www.w3.org/1999/xhtml" >

    <

    head id="Head1" runat="server">

    <

    title>Custom_Query_Report</title>

    </

    head>

    <

    body>

    <table border="0" width="100%">

    <tr>

    <td style="text-align:Left; width:50%; height: 23px;"><span style="font-size: 14pt; color: activecaption; text-decoration: underline">Report Results</span></td>

    <td style="text-align:right; width:50%; height: 23px;"><a href="javascript:self.close('Custom_Query_Report.aspx');">Close Window</a></td>

    </tr>

    </table>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" BorderStyle="Solid" ForeColor="#0000C0" Font-Size="Small" EmptyDataText="No records found" PagerSettings-Mode="NumericFirstLast" BorderColor="Black" BorderWidth="2px" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="gridView1_PageIndexChanging" OnSorting="gridView1_Sorting" PageSize="25">

    <AlternatingRowStyle BackColor="LightGray" />

    <HeaderStyle BackColor="Gray" Font-Bold="True" Font-Names="Verdana" Font-Size="Smaller" />

    <PagerStyle BackColor="DarkGray" Font-Names="Verdana" Font-Size="Smaller" />

    <RowStyle Font-Names="Verdana" Font-Size="Smaller" />

    <Columns>

    <asp:BoundField Datafield="A" HeaderText="A" SortExpression="A" />

    <asp:BoundField Datafield="Z" HeaderText="Z" SortExpression="Z" />

    <asp:BoundField Datafield="SYS#" HeaderText="SYS#" SortExpression="SYS#" />

    <asp:BoundField Datafield="TYPE" HeaderText="TYPE" SortExpression="TYPE" />

    <asp:BoundField Datafield="MOD" HeaderText="MOD" SortExpression="MOD" />

    <asp:BoundField Datafield="PI" HeaderText="PI" SortExpression="PI" />

    <asp:BoundField Datafield="MOD_NO" HeaderText="MOD_NO" SortExpression="MOD_NO" />

    <asp:BoundField Datafield="CD_PEC" HeaderText="CD_PEC" SortExpression="CD_PEC" />

    <asp:BoundField Datafield="IN_DATE" HeaderText="IN_DATE" SortExpression="IN_DATE" />

    <asp:BoundField Datafield="OUT_DATE" HeaderText="OUT_DATE" SortExpression="OUT_DATE" />

    <asp:BoundField Datafield="COMMENTS" HeaderText="COMMENTS" SortExpression="COMMENTS" />

    <asp:BoundField Datafield="STATUS" HeaderText="STATUS" SortExpression="STATUS" />

    <asp:BoundField Datafield="XCON_TYPE" HeaderText="XCON_TYPE" SortExpression="XCON_TYPE" />

    <asp:BoundField Datafield="MEGAROUTE" HeaderText="MEGAROUTE" SortExpression="MEGAROUTE" />

    <asp:BoundField Datafield="MG_CLLI" HeaderText="MG_CLLI" SortExpression="MG_CLLI" />

    <asp:BoundField Datafield="CD" HeaderText="CD" SortExpression="CD" />

    <asp:BoundField Datafield="PO" HeaderText="PO" SortExpression="PO" />

    </Columns>

    <PagerSettings Mode="NumericFirstLast" />

    </asp:GridView>

    <br />

    <asp:Label ID="Lable1" runat="server" ForeColor="Red"></asp:Label><br />

    </div>

    </form>

    </

    body>

    </

    html>
  • HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    05-07-2007, 5:42 AM
    Locked

    hi, reyan ....

     

    i m facing when i m implement this code on my application.....it gives error that unable to find  " ASC " column 

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    05-19-2007, 8:54 PM
    Locked
    • Member
      2 point Member
    • donjuanwu
    • Member since 05-20-2007, 12:03 AM
    • Posts 1

    Ryan,

    When I click on the Page object button I see this function get called GridView_PageIndexChanging(Ojbect sender, GridViewPageEventArgs e)

    {

    BemsIDInfoGridView.PageIndex = e.NewPageIndex;

    BemsIDInfoGridView.DataBind();

    }

    however, my gridview object disappear. I added this line of code to the above function

    this.BemsIDInfoGridView.Visible = true;

    still nothing show up when I click on the Paging object button including the GridView object. Do you have any idea why my Paging code is not working.

    Thanks,

    donjuanwu

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    05-28-2007, 3:24 PM
    Locked
    • Member
      18 point Member
    • jonefer
    • Member since 01-11-2007, 10:13 PM
    • Posts 35

    Let me preface this by - Like donjuanwu... my gridview disappears when I click any header - HOWEVER, I am not using any paging, my business did not request paging, they prefer to scroll down the page. 

     

    Please help me with the 2 following strings....

    This is how they appear in your post: 

    private string GridViewSortDirection

        {

            get { return ViewState["SortDirection"] as string ?? "ASC"; }

            set { ViewState["SortDirection"] = value; }

        }

     

         

     

     

    For my application, I have to translate them to Visual Basic

    So they look like this:

     

    Private Property GridViewSortDirection() As String
     Get
              Return ViewState("SortDirection") as String ?? "ASC"
     End Get
     Set (ByVal Value As String)
              ViewState("SortDirection") = value
     End Set
    End Property
     
     
     
        Private Property GridViewSortBLOCKED EXPRESSION As String
         Get
              Return ViewState("SortBLOCKED EXPRESSION as String ?? String.Empty
         End Get
         Set (ByVal Value As String)
              ViewState("SortBLOCKED EXPRESSION = value
         End Set
        End Property
     

     I get a blue squiggly line under both of these --------> String ?? "ASC"   and   as String ?? String.Empty

    "End of Statement Expected"

     

    But clearly you are running this code as is.... what do I need to do to adjust them so they work for me? 

     

    I have successfully translated the other necessary components.

     

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    05-29-2007, 12:16 PM
    Locked
    • Member
      9 point Member
    • thekid
    • Member since 11-15-2006, 8:59 PM
    • Posts 10

    StrongTypes:

    Here's the live working example with source code: GridView Sorting/Paging w/o a DataSourceControl DataSource

    I had to tweak the code a little to make sorting work in both directions.

    Enjoy.
    Ryan

     

     

    This is almost exactly what I am looking for, is this possible via stored procedures too? 

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    06-12-2007, 5:31 AM
    Locked
    • Member
      6 point Member
    • Raafat4
    • Member since 05-26-2007, 9:42 AM
    • Posts 3

    Hi,

    I applied the code which you wrote it,There are no Errors but the Sorting failed.

    Att1. I am using Microsoft Enterprise Library.

    Att2.I am working as NTier and my code available on Data Access Layer.

    Can you help me in this regards.

    Thank you in advance

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    06-14-2007, 4:48 AM
    Locked
    • Member
      2 point Member
    • neoleela
    • Member since 06-14-2007, 8:38 AM
    • Posts 1

    The new code worked well. If the datasource is not from database, then we have to use one line of code to add the grid's datasource into viewstate.

    ViewState["gridview_DataSource"] = gridview.DataSource;

    And later, whereever datatable is needed, add this line of code.

    (DataTable)ViewState["gridMyCases_DataSource"]

    The solution just works too good. Thanks Ryan.

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    06-15-2007, 5:29 AM
    Locked
    • Member
      2 point Member
    • aishas
    • Member since 06-06-2007, 7:07 AM
    • Posts 2

    test

Page 11 of 13 (186 items) « First ... < Previous 9 10 11 12 13 Next >