简体   繁体   中英

How to add dynamic paging in grid view using asp.net

I want to display only 10 records on one page, then rest of the records it should display on second and third page. When user click 2 it should display the next 10 records. Here I am entering data to gridview dynamically.

this is my code

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 code

<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>

I am new to grid view and asp.net.

您可以按照给定的示例使用AllowPagingAllowPaging记录PageSize的记录,谢谢

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM