My markup is:
<asp:GridView ID="grvGroup" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True"
PageSize="10" CssClass="mydatagrid"
OnRowDataBound="grvGroup_RowDataBound" OnRowDeleting="grvGroup_RowDeleting" OnRowCommand="grvGroup_RowCommand"
DataKeyNames="No" OnPageIndexChanging="grvGroup_PageIndexChanging" PagerStyle-HorizontalAlign="Center"
PagerSettings-Mode="NumericFirstLast"
PagerSettings-Visible="True">
<Columns>
<asp:ButtonField CommandName="Select" ButtonType="Button" ControlStyle-CssClass="Edit_btn" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="5px" HeaderStyle-Width="5px" />
<asp:BoundField HeaderText="Sl.No." ItemStyle-Width="5px" HeaderStyle-Width="5px" DataField="No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="InvoiceNumber" ItemStyle-Width="100px" HeaderStyle-Width="100px" DataField="InvoiceNumber" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Vou Date" ItemStyle-Width="80px" HeaderStyle-Width="80px" DataField="CDate" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="False" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="BankName" ItemStyle-Width="130px" HeaderStyle-Width="130px" DataField="BankName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
</Columns>
<EmptyDataRowStyle HorizontalAlign="Center" />
<HeaderStyle CssClass="header" />
<PagerStyle CssClass="pager" />
<RowStyle CssClass="rows" />
<AlternatingRowStyle CssClass="row-alt" />
</asp:GridView>
This is my code behind file:
private void BindData(long refNo = 0, int pageIndex = 0)
{
try
{
int pageSize = grvGroup.PageSize; // Get the page size
DataSet Data = CommonClass.DSWriteToTable(
_GetBank( refNo, pageIndex, pageSize)
);
if (Data != null && Data .Tables.Count > 1)
{
DataTable dt = Data .Tables[1];
if (dt.Rows.Count > 0)
{
grvGroup.AllowPaging = true;
grvGroup.PageSize = 10;
grvGroup.Visible = true;
grvGroup.PageIndex = pageIndex;
grvGroup.DataSource = dt;
grvGroup.DataBind();
}
else
{
grvGroup.Visible = false;
}
if (Data .Tables.Count > 2 && Data .Tables[2].Rows.Count > 0)
{
int totalRows = Convert.ToInt32(Data .Tables[2].Rows[0]["Counts"]);
int totalPages = (int)Math.Ceiling((double)totalRows / pageSize);
ViewState["TotalPages"] = totalPages;
}
}
}
catch (Exception ex)
{
}
}
While if the data set having more than 10 rows, it is showing row numbers under the grid if we use pagination set it is not showing any row number so the pagination is not happening
My markup is:
<asp:GridView ID="grvGroup" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True"
PageSize="10" CssClass="mydatagrid"
OnRowDataBound="grvGroup_RowDataBound" OnRowDeleting="grvGroup_RowDeleting" OnRowCommand="grvGroup_RowCommand"
DataKeyNames="No" OnPageIndexChanging="grvGroup_PageIndexChanging" PagerStyle-HorizontalAlign="Center"
PagerSettings-Mode="NumericFirstLast"
PagerSettings-Visible="True">
<Columns>
<asp:ButtonField CommandName="Select" ButtonType="Button" ControlStyle-CssClass="Edit_btn" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="5px" HeaderStyle-Width="5px" />
<asp:BoundField HeaderText="Sl.No." ItemStyle-Width="5px" HeaderStyle-Width="5px" DataField="No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="InvoiceNumber" ItemStyle-Width="100px" HeaderStyle-Width="100px" DataField="InvoiceNumber" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Vou Date" ItemStyle-Width="80px" HeaderStyle-Width="80px" DataField="CDate" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="False" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="BankName" ItemStyle-Width="130px" HeaderStyle-Width="130px" DataField="BankName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
</Columns>
<EmptyDataRowStyle HorizontalAlign="Center" />
<HeaderStyle CssClass="header" />
<PagerStyle CssClass="pager" />
<RowStyle CssClass="rows" />
<AlternatingRowStyle CssClass="row-alt" />
</asp:GridView>
This is my code behind file:
private void BindData(long refNo = 0, int pageIndex = 0)
{
try
{
int pageSize = grvGroup.PageSize; // Get the page size
DataSet Data = CommonClass.DSWriteToTable(
_GetBank( refNo, pageIndex, pageSize)
);
if (Data != null && Data .Tables.Count > 1)
{
DataTable dt = Data .Tables[1];
if (dt.Rows.Count > 0)
{
grvGroup.AllowPaging = true;
grvGroup.PageSize = 10;
grvGroup.Visible = true;
grvGroup.PageIndex = pageIndex;
grvGroup.DataSource = dt;
grvGroup.DataBind();
}
else
{
grvGroup.Visible = false;
}
if (Data .Tables.Count > 2 && Data .Tables[2].Rows.Count > 0)
{
int totalRows = Convert.ToInt32(Data .Tables[2].Rows[0]["Counts"]);
int totalPages = (int)Math.Ceiling((double)totalRows / pageSize);
ViewState["TotalPages"] = totalPages;
}
}
}
catch (Exception ex)
{
}
}
While if the data set having more than 10 rows, it is showing row numbers under the grid if we use pagination set it is not showing any row number so the pagination is not happening
Share Improve this question edited Mar 19 at 17:55 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 19 at 12:01 DevDev 1,83616 silver badges34 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 5To use the built in GridView paging, then your dataset has to return ALL rows, not just one page of data.
Hence, here is a working example:
Markup:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
CssClass="table table-hover" Width="800px"
AllowPaging="true" PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging"
>
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Descripiton" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<button id="cmdEdit" runat="server"
onserverclick="cmdEdit_ServerClick"
>
<span aria-hidden="true" class="fa fa-pencil-square-o fa-lg"></span>
</button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="GridPager" VerticalAlign="Bottom" BorderWidth="2" />
</asp:GridView>
And code behind to load the Gridview:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
string strSQL =
@"SELECT * FROM tblHotelsA
ORDER BY HotelName";
GridView1.DataSource = General.MyRst(strSQL);
GridView1.DataBind();
}
And the code behind for the OnPageIndexChanging event is thus:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
LoadData();
}
And the result is now this:
--
Edit: using custom paging
===
You can also use custom paging, and this WILL allow you to use custom paging code.
Requirements:
You must be using asp 4.5 or later.
Set the GridView to with both AllowPaging="true" and also AllowcustomPaging = true.
And to set the number of rows, we use a new feature called:
VirtualItemCount
Here is a working example.
In this example, we have 6.7 million rows. Needless to say, we certainly don't want to have a "page number" control for each page, since with 6.7 million rows, and a page size of 16, then that would be 423273 pages!
So, this GridView:
<asp:GridView ID="GVHistory" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover"
AllowPaging="true"
AllowCustomPaging="true"
PageSize="16"
OnPageIndexChanging="GVHistory_PageIndexChanging"
Width="60%">
<Columns>
<asp:BoundField DataField="DateStamp" HeaderText="Date"
DataFormatString="{0:dd/MM/yyyy hh:mm tt}" ItemStyle-Width="160px" />
<asp:BoundField DataField="CompanyEmployeeID" HeaderText="Emp ID" ItemStyle-Width="80px" />
<asp:BoundField DataField="ComputerNetworkName" HeaderText="Computer Name" />
<asp:BoundField DataField="Note" HeaderText="Notes" />
</Columns>
<PagerTemplate>
<asp:Button ID="cmdFirst" runat="server" Text="First"
CommandName="Page" CommandArgument="First"
CssClass="btn myshadow"
style="float:left"
/>
<asp:Button ID="cmdPrev" runat="server" Text="Previous"
CommandName="Page" CommandArgument="Prev"
CssClass="btn myshadow"
style="margin-left:10px;float:left"
/>
<div style="float:left;margin-left:20px;margin-top:8px">
Page
<asp:Label ID="lblPageNum" runat="server" Text=''>
</asp:Label>
Of
<asp:Label ID="lblPageCount" runat="server"
Text=''>
</asp:Label>
</div>
<asp:Button ID="cmdNext" runat="server" Text="Next"
CommandName="Page" CommandArgument="Next"
CssClass="btn myshadow"
style="margin-left:15px;float:left"
/>
<asp:Button ID="cmdLast" runat="server" Text="Last"
CommandName="Page" CommandArgument="Last"
CssClass="btn myshadow"
style="margin-left:10px;float:left"
/>
</PagerTemplate>
</asp:GridView>
note the custom template. We simply dropped in 4 buttons (First, Previous, Next, and Last. Take CLOSE note of the command arguments used for the buttons.
So, now our code behind has to first "count" the total number of rows, and feed that number to the VirtualItemCount.
Note that that database pager code starts at page 1, but the GV starts at page 0.
Hence this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dtRowCount =
General.MyRst("SELECT COUNT(*) FROM dbo_ContactHistory",
Properties.Settings.Default.AxisMIS);
GVHistory.VirtualItemCount = (int)dtRowCount.Rows[0][0];
LoadData(0);
}
}
void LoadData(int PageNumber)
{
GVHistory.DataSource = GetData(PageNumber);
GVHistory.DataBind();
Label lblPage = (Label)GVHistory.BottomPagerRow.FindControl("lblPageNum");
Label lblPageCount = (Label)GVHistory.BottomPagerRow.FindControl("lblPageCount");
lblPage.Text = (PageNumber + 1).ToString();
lblPageCount.Text = (GVHistory.VirtualItemCount / GVHistory.PageSize).ToString();
}
public DataTable GetData(int PageNum)
{
DataTable dtHistory = new DataTable();
string strSQL =
@"SELECT * FROM dbo_ContactHistory
ORDER BY dbo_ContactHistory.DateStamp
OFFSET((@PageNumber - 1) * @RowsPerPage) ROWS
FETCH NEXT @RowsPerPage ROWS ONLY";
SqlCommand cmdSQL = new SqlCommand(strSQL);
cmdSQL.Parameters.Add("@PageNumber", SqlDbType.Int).Value = PageNum + 1;
cmdSQL.Parameters.Add("@RowsPerPage", SqlDbType.Int).Value = GVHistory.PageSize;
dtHistory = General.MyRstP(cmdSQL, Properties.Settings.Default.AxisMIS);
return dtHistory;
}
protected void GVHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex >= 0)
{
GVHistory.PageIndex = e.NewPageIndex;
LoadData(e.NewPageIndex);
}
}
And now we are paging data with 6.7 million rows, and the response time is instant.
Hence this:
Data.Tables[1]
will only contain 10 rows since you send thepageSize
andpageIndex
parameter toDSWriteToTable
. If so there is nothing to page for the gridview, that only works if the amount of rows bound to it is greater than PageSize. – VDWWD Commented Mar 19 at 12:15