0

在页脚网格视图中添加有关总页数和总行数的信息时出现问题,

这是我的 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();  
    }

我的网格视图

这是我的问题,我没有找到添加信息总页数和行数的方法

任何人都可以改进我的代码。

4

1 回答 1

0

在您的绑定方法中调用它

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

于 2019-06-27T03:57:37.487 回答