To handle large datasets like your 2000 rows in a GridView, you can implement paging to display the data across multiple pages. Here’s how you can achieve this in C# ASP.NET WebForms.
Step 1. Enable Paging in the GridView
Enable paging directly on the GridView control by setting its AllowPaging property to true and defining the PageSize.
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging">
</asp:GridView>
Step 2. Bind Data to the GridView
In your code-behind, bind the data to the GridView. Make sure you only bind the data when the page is not being posted back (i.e., on the first load).
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
// Assuming you have a method to fetch data from your data source
DataTable dt = GetDataFromDatabase(); // Replace with your data fetching logic
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetDataFromDatabase()
{
// Example method to get data - replace this with your actual database code
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 1; i <= 2000; i++)
{
dt.Rows.Add(i, "Name " + i);
}
return dt;
}
Step 3. Handle Page Index Changing Event
To navigate between pages, handle the PageIndexChanging event of the GridView.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
Step 4. (Optional) Customize Paging Appearance
If you want to customize the appearance of the paging controls (e.g., showing first, previous, next, and last buttons), you can do this in the GridView's PagerSettings.
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging">
<PagerSettings
Mode="NumericFirstLast"
FirstPageText="First"
LastPageText="Last" />
</asp:GridView>
Complete Example Code
Here’s how the complete implementation would look:
ASP.NET WebForm (ASPX)
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging">
<PagerSettings
Mode="NumericFirstLast"
FirstPageText="First"
LastPageText="Last" />
</asp:GridView>
Code-Behind (C#)
using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
DataTable dt = GetDataFromDatabase();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetDataFromDatabase()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 1; i <= 2000; i++)
{
dt.Rows.Add(i, "Name " + i);
}
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
}
Conclusion
With the above code, you’ll have a GridView that displays 10 rows per page and allows users to navigate through the pages to view all 2000 rows. This approach makes your web application more user-friendly and efficient when dealing with large datasets.