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

    12-30-2006, 12:10 AM
    Locked

    In the gridView_Sorting event you test gridView.DataSource.

    This property is always null on postbacks, isn´t it?

    Does the GridView save its DataSource property at the ViewState or at other place?

    How does the GridView restore its DataSource property after a postback?

    Thanks!!!

     

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

    01-02-2007, 7:17 AM
    Locked
    • Contributor
      4,305 point Contributor
    • ASP.NET Dev
    • Member since 12-09-2006, 7:20 AM
    • Kozhikode, Kerala, India
    • Posts 660

     

    That was a really nice detailed code, I have doubt regarding “PopulatePublishersGridView();” this method will be called in every page load, isn’t it ? Then why should sort the already binded grid. 

     

    Would it be better to call the “PopulatePublishersGridView();” on page load if not post back and create a dynamic query for sorting rather than dataview sort.

     

    Please let me know if the first method was followed for any particular reason.

     

    Sorry if I sound foolish :) 

    Regards,
    Sandeep Kumar. M

    MCPD - Web Developer
    --------------------------------------------------
    Software Associates, Calicut, Kerala
    http://www.softwareassociates.in



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

    01-03-2007, 12:45 AM
    Locked
    • Contributor
      4,221 point Contributor
    • moredotnet
    • Member since 11-24-2006, 12:07 PM
    • Millenium City
    • Posts 781

    Firstly, I dont understand why you are trying to make life little tougher by sorting without a datasource :)

    Anyways, I think it may be done using Javascript, using DOM, but then, it will eat up lots of brains to code it.

    This link may help you for sorting using javascript http://javascript.internet.com/forms/sort-data-table.html

    Instead of the table, you need to pickup the gridview's id.

    moredotnet

     

     

    Vishal Khanna



    ASP.NET|C#|AJAX|SQL|Design Patterns|Interview FAQs
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    01-03-2007, 12:13 PM
    Locked
    • Member
      272 point Member
    • cm1jm1
    • Member since 07-27-2004, 11:32 AM
    • Posts 89

    I first want to say I really appreciated your work. First rate. I really need to know though, do you have an example of someone who had a stored procedure used to fill a datagrid and then implement the sorting and paging code? Your example is great if you load data on Page_Load. But I have a search page that fills a dataset from user selection and then the grid needs to be paged and sorted. Thanks.

     Cordell

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

    01-09-2007, 7:48 PM
    Locked
    • Member
      17 point Member
    • Smithar
    • Member since 04-06-2005, 6:00 PM
    • Posts 4
    paulafernandes:

      The problem is that when I use the sorting, the selectindex that I retrieve is not from the sorted gridview.

    How can I retrieve the right one????

    Thank's

    Paula 

     

    I have the same problem. Please help.

     

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

    01-10-2007, 9:31 AM
    Locked
    • Member
      193 point Member
    • Zenuke
    • Member since 10-18-2006, 1:28 PM
    • Posts 260

    I am also having an isseu when attempting to use a dataset. I have the dataview set to the correct table but the dataset is not being passed fro mthe gridview onsorting command. I dont have paging so I left out the paging code.

     

    GridViewSortExpression = e.SortExpression

    GridView1.DataSource = SortDataSet(GridView1.DataSource) <--  this does not seem to work.

    GridView1.DataBind()

    Protected

    Function SortDataSet(ByVal ds As System.Data.DataSet) As DataView

    Dim dataView As New DataView(ds.Tables(0)) <-- getting error. "Object reference not set to an instance of an object."

    If GridViewSortExpression <> String.Empty Then

    dataView.Sort =

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

    Return dataView

    else

    Return New DataView()

    End If

    End Function

     

    Any IDeas?

     

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

    01-10-2007, 10:55 AM
    Locked
    • Member
      193 point Member
    • Zenuke
    • Member since 10-18-2006, 1:28 PM
    • Posts 260
    well i made it work by just using a datatable except...It does not remember the sorting criteria if i do any other action (ie. select a row and save it etc)
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    01-12-2007, 5:52 AM
    Locked
    • Member
      6 point Member
    • jimatwork
    • Member since 01-09-2007, 5:50 PM
    • Posts 4

    Hi Ryan,

     I need to connect a datatable w/o a datasource control to a gridview, but I have one question about your example.  You are retrieving the data on every post, I thought the datatable should be created once and the gridview should handle persisting the data between posts.  What am I missing?

     

    Thank you, Jim

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

    01-17-2007, 4:21 AM
    Locked
    • Member
      6 point Member
    • venkatm76
    • Member since 01-10-2007, 7:32 AM
    • Posts 3

    Hi

    I am new to ASP.net(vb)

    I was trying my hand with gridview and I was successfull with indexing(paging) but with sorting I am getting the same direction for sorting.

     

    What could be the reply..

     

    My thanks in advance 

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

    01-17-2007, 5:32 AM
    Locked
    • Member
      2 point Member
    • sarada
    • Member since 01-17-2007, 10:02 AM
    • Posts 1

    Hi

    I will like you to put your hands in some real good way so tat you wont be having any problem with sorting and paging simultaniously

    For sorting wat you need to do is pull the DataSet to DataView and sort it with <dataview>.Sort=e.SortExpression and <datagrid>.DataSource=<daatview>Idea

    For paging dont use the default paging as given by .net rather put small code so tat it will work fine with sorting.first put the paging option to true. Put some good image button for next page and first page and on the click event put the following code

    (for next page)

    if

    (<datagrid>.CurrentPageIndex<<datagrid>.PageCount-1)

    {

    <datagrid>.CurrentPageIndex=<datagrid>.CurrentPageIndex+1;

    <datagrid>.DataSource =<dataview> ;

    dg_ticket.DataBind();

    lbl_page.Text=dg_ticket.CurrentPageIndex+1 +" of "+ dg_ticket.PageCount;

     

    }

    (for previous page)

    if

    (<datagrid>.CurrentPageIndex!=0)

    {

    <datagrid>.CurrentPageIndex=<datagrid>.CurrentPageIndex-1;

    <datagrid>.DataSource = <dataview> ;

    <datagrid>.DataBind();

     

    }

    Hope this will help u.... have happy coding.....

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

    01-17-2007, 10:14 PM
    Locked
    • Member
      6 point Member
    • venkatm76
    • Member since 01-10-2007, 7:32 AM
    • Posts 3

    Hi

    I am okay with Paging, Problem is only with the sorting part ,

    Can U give a VB.net example for the same( I could not understand <datagrid>.DataSource=<daatview>)

    I tried using the AccessDatasource all worked fine with paging and sorting

    1. Now I have a Combo box which is already populated and the grid gets loaded with all coloums  with out any filter.
    2. Now when the users selects a value from the combo box and presses the button . With in the Button SUB i change the select command for the gridview and bingo all is cool the gridview has now the filtered data
    3. Now when I click the paging there comes the fun Second page is shown but with no filter

    What's the solution friends

    Sad 

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

    01-24-2007, 6:54 PM
    Locked

    I'm trying to sort a Gridview using a DataSet as above.

    Dim ds As New DataSet

    Dim dv As New DataView

    ds = gvTableView.DataSource

    dv = ds.Tables(0)

    The last line generates a compile error "Error 1 Value of type 'System.Data.DataTable' cannot be converted to 'System.Data.DataView'. C:\AA_DEV\PROJECTS\WEB\AAFOS_WEB\TableView.aspx.vb 70 14 C:\...\AAFOS_WEB\'

    In addition why doesn't the following work

    If Session("sortDir").ToString() = "DESC" Then

    gvTableView.Sort(e.SortExpression, SortDirection.Ascending)

    Else

    gvTableView.Sort(e.SortExpression, SortDirection.Ascending)

    End If


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

    01-25-2007, 7:04 PM
    Locked
    • Member
      286 point Member
    • Ali.Syed
    • Member since 10-12-2006, 8:22 PM
    • Posts 98

    Hello there,

    I'm trying to use your logic. First question, does this logic causes postback. Second I'm using the following code (vb.net), but It is doing nothing. The if condition is always false, i.e. Datatable is always null. Please help. Thanks.

    Protected

    Sub gvBli_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvBli.Sorting

    'tbSortid.Text = e.SortExpression

    'Refreshbligrid()

    Dim dataTable As DataTable = gvBli.DataSource

    If Not dataTable Is Nothing Then

    Dim dataView As DataView = New DataView(dataTable)

    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionTosql(e.SortDirection)

    gvBli.DataSource = dataView

    gvBli.DataBind()

    End If

    End Sub

     

    Private Function ConvertSortDirectionTosql(ByVal sortDirection As SortDirection)

    Dim newSortDirection As String = ""

    Select Case sortDirection

    Case sortDirection.Ascending

    newSortDirection =

    "ASC"

    Case sortDirection.Descending

    newSortDirection =

    "DESC"

    End Select

    Return newSortDirection

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

    01-26-2007, 3:06 AM
    Locked
    • Member
      30 point Member
    • Aouie
    • Member since 01-25-2007, 9:52 AM
    • Philippines
    • Posts 16

    what about in VB using SQL SERVER 2005, how can i do sorting / paging on GridView w/o DatasourceControl? or with Datasource control enable the sorting and paging using the EnableSortingCallback feature?

    I have three Dropdownlist, everytime i sort the gridview with enableSortingCallback=true, it always sort back to the whole data? what will i going to do?

     

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

    01-29-2007, 11:07 AM
    Locked
    • Participant
      1,283 point Participant
    • gogetsome
    • Member since 09-04-2004, 11:33 AM
    • Nashville
    • Posts 301

    Hello, wow! this topic is popular. I need help with my code for paging and sorting. It is not working and I get this error message:

    Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'. 

    on this line:

    gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

    False)

     

    I really need some help, please. What is wrong with this code?

     

    'sub used to search for available quotes based on user supplied criteria

    Private Sub SearchQuotes()

    'set connection strig

    Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("quoteConnectionString").ToString)

    'set SPROC command object

    Dim objCmd As SqlCommand = New SqlCommand("BB_QH_QuoteLookup", objConn)

    objCmd.CommandType = CommandType.StoredProcedure

    'stored procedure parameters

    Dim SearchQuoteNumber As SqlParameter = objCmd.Parameters.Add("@txtSearchQuoteNumber", SqlDbType.VarChar, 12)

    SearchQuoteNumber.Value = txtSearchQuotesQuoteNumber.Text.Trim()

    Dim SearchCustnmbr As SqlParameter = objCmd.Parameters.Add("@txtSearchCustnmbr", SqlDbType.Char, 15)

    SearchCustnmbr.Value = txtSearchQuotesCustnmbr.Text.Trim()

    Dim SearchQuotedFor As SqlParameter = objCmd.Parameters.Add("@ddlSearchQuotedFor", SqlDbType.Char, 21)

    SearchQuotedFor.Value = lblSearchQFUserName.Text.Trim()

    Dim SearchAreaManager As SqlParameter = objCmd.Parameters.Add("@ddlSearchAreaManager", SqlDbType.Char, 21)

    SearchAreaManager.Value = lblSearchAMUserName.Text.Trim()

    Dim SearchEngineer As SqlParameter = objCmd.Parameters.Add("@ddlSearchEngineer", SqlDbType.Char, 21)

    SearchEngineer.Value = lblSearchENUserName.Text.Trim()

    Dim SearchCreatedBy As SqlParameter = objCmd.Parameters.Add("@ddlSearchCreatedBy", SqlDbType.Char, 21)

    SearchCreatedBy.Value = ddlSearchQuotesCreatedBy.SelectedValue.Trim()

    'open the connection to the db

    objConn.Open()

    'set dataadapter and dataset and execute the sproc

    Dim objDA As New SqlDataAdapter(objCmd)

    Dim objDS As New DataTable("QuoteNumber")

    objDA.Fill(objDS)

    'fill gvQuotes with results of search

    Dim dataTableRowCount As Integer = objDS.Rows.Count

    If dataTableRowCount > 0 Then

    gvQuotes.DataSource = objDS

    gvQuotes.DataBind()

    End If

    'close the connection to the db

    objConn.Close()

    'give the user a message if the search did not return any results

    If gvQuotes.Rows.Count = 0 Then

    lblSearchQuotesMessage.Visible =

    True

    lblSearchQuotesMessage.Text =

    "Your Search did not return any results."

    Else

    'if results are true then show the gvQuotes

    gvQuotes.Visible =

    True

    End If

    End Sub

     

    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 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 gvQuotes_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

    gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

    True)

    gvQuotes.PageIndex = e.NewPageIndex

    gvQuotes.DataBind()

    End Sub

    Protected Sub gvQuotes_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvQuotes.Sorting

    GridViewSortExpression = e.SortExpression

    Dim pageIndex As Integer = gvQuotes.PageIndex

    gvQuotes.DataSource = SortDataTable(gvQuotes.DataSource,

    False)

    gvQuotes.DataBind()

    gvQuotes.PageIndex = pageIndex

    End Sub
Page 10 of 13 (186 items) « First ... < Previous 8 9 10 11 12 Next > ... Last »