如何使用asp.net在网格视图中添加动态分页

时间:2016-10-04 09:43:04

标签: c# asp.net gridview datatable

我想在一个页面上只显示10条记录,然后在第二页和第三页上显示其余的记录。当用户单击2时,它应显示接下来的10条记录。我在这里动态地将数据输入到gridview。

这是我的代码

protected void Page_Load(object sender, EventArgs e)
    {
        // data load to grid view
        loadDataTable();

    }

    private void loadDataTable()
    {
        DataSet ds = new DataSet();
        DataTable dt;
        DataRow dr;

        DataColumn date;
        DataColumn designation;
        DataColumn experience;
        DataColumn location;
        DataColumn nationality;
        DataColumn details;

        dt = new DataTable();

        date = new DataColumn("Date");
        designation = new DataColumn("Designation");
        experience = new DataColumn("Experience");
        location = new DataColumn("Location");
        nationality = new DataColumn("Nationality");
        details = new DataColumn("Details");

        dt.Columns.Add(date);
        dt.Columns.Add(designation);
        dt.Columns.Add(experience);
        dt.Columns.Add(location);
        dt.Columns.Add(nationality);
        dt.Columns.Add(details);

        string cs = ConfigurationManager.ConnectionStrings["connectionStringDb"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("spGetJobPost_tb",con);

            cmd.CommandType = CommandType.StoredProcedure;

            con.Open();
            SqlDataReader rd = cmd.ExecuteReader();

            while(rd.Read())
            {
                dr = dt.NewRow();
                //DateTime dat = DateTime.ParseExact(rd["Date"].ToString(),"dd/MM/yyyy",null);
                string dates= ((DateTime)rd["Date"]).ToString("dd-MM-yyyy");
                dr["Date"] = dates;
                dr["Designation"] = rd["job_title"].ToString();
                dr["Experience"] = rd["experience"].ToString();
                dr["Location"] = rd["location"].ToString();
                dr["Nationality"] = rd["nationality"].ToString();
                dr["Details"] = "<a href='Details.aspx?id="+rd["Id"].ToString()+"' target='_blank'>View Details</a>";

                dt.Rows.Add(dr);
            }
        }
        ds.Tables.Add(dt);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

asp.net代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                        <Columns>
                            <asp:BoundField DataField="Date" HeaderText="Date" />
                            <asp:BoundField DataField="Designation" HeaderText="Designation" />
                            <asp:BoundField DataField="Experience" HeaderText="Experience" />
                            <asp:BoundField DataField="Location" HeaderText="Location" />
                            <asp:BoundField DataField="Nationality" HeaderText="Nationality" />
                            <asp:BoundField HeaderText="Details" HtmlEncode="false" DataField="Details" />
                        </Columns>

                    </asp:GridView>

我是网格视图和asp.net的新手。

1 个答案:

答案 0 :(得分:1)

根据给定示例,您可以将AllowPagingPageSize用于每页的记录,谢谢

<asp:GridView runat="server" ID="studentsGrid" AllowPaging="true" PageSize="4">