A GridView is most often used to display the data retrieved from the database in Tabular form with features of Gridview like data paging, sorting and auto formats.
You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with paging enabled.
First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.
To enable the paging in GridView control select True from the dropdown list of AllowPaging property of GridView control as shown in the above image.
It will add AllowPaging="True" in HTML source code of Gridview.
Next step is to bind the data with Gridview control and handle the GridView paging event.
To bind the PageIndexChanging of GridView control, double click on the PageIndexChanging event in the properties of Gridview. It will add the event in HTML source code as well as C# code.
You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with paging enabled.
First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.
<asp:GridView id="GridView1" runat="server"></asp:GridView>
To enable the paging in GridView control select True from the dropdown list of AllowPaging property of GridView control as shown in the above image.
It will add AllowPaging="True" in HTML source code of Gridview.
Next step is to bind the data with Gridview control and handle the GridView paging event.
To bind the PageIndexChanging of GridView control, double click on the PageIndexChanging event in the properties of Gridview. It will add the event in HTML source code as well as C# code.
<asp:GridView ID="GridView1"You can set the PageSize value as per your requirement.
runat="server"
CellPadding="2"
AllowPaging="True"
PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging"
AutoGenerateColumns="False"
Width="500px">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="Product Name">
<HeaderStyle HorizontalAlign="Left" Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="UnitsInStock" HeaderText="Units In Stock">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="UnitPrice" HeaderText="Price Per Unit">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindGridView();
}
public void bindGridView()
{
// string variable to store the connection string
// defined in ConnectionStrings section of web.config file.
string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
// object created for SqlConnection Class.
SqlConnection mySQLconnection = new SqlConnection(connStr);
// if condition that can be used to check the sql connection
// whether it is already open or not.
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
SqlCommand mySqlCommand = new SqlCommand("select ProductID, ProductName, UnitsInStock, UnitPrice from products", mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
// if condition that can be used to check the sql connection
// if it is open then close it.
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView();
}
No comments:
Post a Comment