在页脚gridview中添加有关总页数和总行数的信息时,我遇到问题
这是我的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();
}
This my problem, I didnt find a way to add information total pages and rows
任何人都可以改善我的代码。
答案 0 :(得分:0)
在绑定方法中调用它
int TotalRecord = ds.Rows.Count(); //This is total number of records in gridview
GridView1.DataSource = ds;
GridView1.DataBind();
您以后可以在标签中调用Totalrecord
或您希望在网格视图中显示的内容,也请参考以下内容:https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridviewrowcollection.count?view=netframework-4.8