Count total rows of gridview with pagination

前端 未结 12 822
失恋的感觉
失恋的感觉 2020-12-03 12:02

I have a GridView with paging. When I try to count gridview row with gridview.rows.count, It gives me row count for current page only.

How can I get total rows of G

相关标签:
12条回答
  • 2020-12-03 12:07

    Try below code

    int _TotalRecs = ((System.Data.DataView)GridView.DataSource).Count;
    int _CurrentRecStart = GridView.PageIndex * GridView.PageSize + 1;
    int _CurrentRecEnd = GridView.PageIndex * GridView.PageSize + GridView.Rows.Count;
    
    lblTitle.Text = string.Format("Displaying {0} to {1} of {2} records found", _CurrentRecStart, _CurrentRecEnd, _TotalRecs);
    

    For e.g. Output will be like below....

    Displaying 1 to 15 of 67 records found.

    0 讨论(0)
  • 2020-12-03 12:12

    Yes I think using PagedDataSource is a better option. I'm using it.

    PagedDataSource pds = new PagedDataSource();
    pds.DataSource = dt_main.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 8;
    
    int currentPage;
    
    if (Request.QueryString["page"] != null)
    {
        currentPage = Int32.Parse(Request.QueryString["page"]);
    }
    else
    {
        currentPage = 1;
    }
    
    pds.CurrentPageIndex = currentPage - 1;
    Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
    
    if (!pds.IsFirstPage)
    {
        linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);
    }
    
    if (!pds.IsLastPage)
    {
        linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);
    }
    gridMain.DataSource = pds;
    gridMain.DataBind();
    
    0 讨论(0)
  • 2020-12-03 12:14
    <font-color='red'><b>dim dt as datatable<Br>
    dt=gridview1.datasource<br>
    msgbox (dt.rows.count)<b></font>
    
    0 讨论(0)
  • 2020-12-03 12:17

    If you are binding using the datatable or other datasource you can show the total records from the datasource. For example

      if (dtLog != null && dtLog .Rows.Count >= 0)
                {
                    lblTotal.Text = "Total " + Convert.ToString(dtLog .Rows.Count) + " records.";
                }
    
    0 讨论(0)
  • 2020-12-03 12:25

    yes, that's true, it will only return the current page rows only. If you really want to get the total row count, you need to get from your datasource instead.

    Like... if you have DataTable then it will be like...

    DataTable.Rows.Count
    
    0 讨论(0)
  • 2020-12-03 12:26
                   int a = grdvw.PageIndex;
                   int rowcount=0;
    
                    for (int i = 0; i < grdvw.PageCount; i++)
                    {
                        grdvw.SetPageIndex(i);
                        foreach (GridViewRow row in grdvw.Rows)
                        {
                            if (row.RowType == DataControlRowType.DataRow)
                            {
                              rowcount++;
                            }
                        }
                   }
    
                 grdvw.SetPageIndex(a);
    
    0 讨论(0)
提交回复
热议问题