c# - 如何在页脚gridview c#中添加信息总页数和行数
问题描述
在页脚网格视图中添加有关总页数和总行数的信息时出现问题,
这是我的 HTML 代码;
<asp:GridView ID="GridView1" runat="server"
onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="True"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" AutoGenerateColumns="False" Width="500px">
<Columns>
<asp:BoundField HeaderText="ProductId" DataField="ProductId" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="ProductName" DataField="ProductName">
<ItemStyle Width="300px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="SupplierId" DataField="SupplierId">
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="7" Mode="NumericFirstLast" />
<RowStyle ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
这个aspx代码:
public void BindData()
{
string strConnection = @"Data Source=.\sa;Initial Catalog=Northwind;Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select ProductId, ProductName, SupplierId from Products", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
//Script row 10
int rowcount = ds.Tables[0].Rows.Count;
int remainingCount = 10 - (rowcount % 10);
for (int i = 0; i < remainingCount; i++)
{
DataRow row = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(row);
}
//Script row ~10
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
任何人都可以改进我的代码。
解决方案
在您的绑定方法中调用它
int TotalRecord = ds.Rows.Count(); //This is total number of records in gridview
GridView1.DataSource = ds;
GridView1.DataBind();
您稍后可以Totalrecord
在标签中调用它或您希望在您的 gridview 中使用的东西也可以参考这里:https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridviewrowcollection。 count?view=network-4.8
推荐阅读
- google-apps-script - 将今天的日期添加到演示幻灯片 - 已添加但无法更新
- arrays - 循环遍历多维数组中的列
- django - Django Rest Framework:将用户从视图传递到序列化程序
- regex - WordPress过滤器将nofollow附加到rel
- ios - 处理统一类型标识符以显示给用户
- oracle - 将多个列值合并为一行
- testing - TestCafe:未能完成对 url 的请求
- purescript - 为什么这个函数应用程序会在 purescript 中生成运行时错误?
- android - SQLite 自动增量未插入
- javascript - 使用单键 = 单值和单键 = 多值在数组中过滤数组