This article will show you how you can bind gridview in
asp.net with paging using c#.net. In this I have used sql server as DB and
provider is SqlConnection, SqlDataAdapter and datatable.
Some of my previous articles are as follows: : Single
RadioButton Selection in GridView In Asp.Net Using C#.Net, Bind
and Validate GridView TextBox Value by jQuery In Asp.Net Using C#, Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#, Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Search
and Display Data In GridView From Database Table In Asp.Net Using C#.Net, Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Search
GridView Record on Button Click By Using C#.Net in Asp.Net.
So for this article first we will create a new asp.net
application and add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView
Bind Using DataTable With Paging In Asp.Net Using C#.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns="False" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="3">
<Columns>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Address"
HeaderText="Address"
/>
<asp:BoundField DataField="UserType"
HeaderText="UserType"
/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In this I have added a onpageindexchanging attribute. So here
is the code to bind the gridview.
using System;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
private
void BindGridView()
{
SqlConnection
con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected
void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs
e)
{
GridView1.PageIndex =
e.NewPageIndex;
BindGridView();
}
}
}
|
In above code I have bind the grid view and then added code
for providing paging effect in gridview using c#.net. Please check the below
code to bind the provide the paging in gridview.
protected
void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs
e)
{
GridView1.PageIndex =
e.NewPageIndex;
BindGridView();
}
|
For doing paging we need ti do some gridview property changes. First we will make true to allow paging.
Now for making changing the no of record to display in the grid view here i have made to display only 3 items in a page.
Now we have done run the application and check the output.
DOWNLOAD
0 comments:
Please let me know your view