如何在GridView中管理大量数据

时间:2019-05-07 05:03:49

标签: c# asp.net gridview

我有一个包含10000条记录的表,因此我只想使用存储过程一次只调用15条记录。

在下次通话时,仅拨打下15个Reocord,然后继续...

请帮帮我!! ...如果可能,请提供示例代码和存储过程代码...谢谢!!!

1 个答案:

答案 0 :(得分:0)

  

在gridview中使用分页作为AllowPaging =“ true”,然后使用   OnPageIndexChanging事件并为我们提供PageSize,请参见以下示例

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
                OnPageIndexChanging="OnPageIndexChanging" PageSize="10">
                <Columns>
                    <asp:BoundField ItemStyle-Width="150px" DataField="Column_Name" HeaderText="Header Name" />
                    <asp:BoundField ItemStyle-Width="150px" DataField="Column_Name" HeaderText="Header Name" />
                    <asp:BoundField ItemStyle-Width="150px" DataField="Column_Name" HeaderText="Header Name" />
                </Columns>
            </asp:GridView>
  

现在在Page_Load事件上将gridview与数据库绑定

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGrid();
                }
            }

        private void BindGrid()
        {
            string conStr = @"Your connection string here";
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table_Name"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
            }
        }
  

对于页面,请在OnPageIndexChanging事件上使用newPageIndex

    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }