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
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.
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();
<font-color='red'><b>dim dt as datatable<Br>
dt=gridview1.datasource<br>
msgbox (dt.rows.count)<b></font>
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.";
}
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
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);