How To Change Page Size of Grid view in run time
First we create a table with 2 columns (roll,name) where roll is Primary key, int datatype and auto increment and name is varchar(50)
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="stylishgridview.aspx.cs"
Inherits="stylishgridview"
%>
<!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>Untitled Page</title>
</head>
<body >
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"
runat="server">
</asp:ScriptManager>
<asp:GridView ID="grd1" runat ="server"
Width="600px"
Height ="600px"
ShowHeader="true"
ShowFooter="true"
AutoGenerateColumns ="false"
AllowPaging="true"
onpageindexchanging="grd1_PageIndexChanging"
onrowcreated="grd1_RowCreated"
Font-Bold="True"
Font-Size="Medium"
ForeColor="Black" AllowSorting="true"
Font-Names="Verdna"
>
<Columns >
<asp:TemplateField >
<HeaderStyle ForeColor="Blue" Font-Bold
="true" />
<HeaderTemplate
>
Sr. No
</HeaderTemplate>
<ItemTemplate>
<div style="width :100%;">
<asp:Label ID="srno" runat ="server"
Text='<%#
Container.DataItemIndex +1 %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderStyle ForeColor="Blue" Font-Bold
="true" />
<HeaderTemplate>
Roll
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtrollno"
runat ="server"
Text='<%#
Bind("Roll") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblrollno" runat
="server" Text='<%#
Bind("Roll") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderStyle ForeColor="Blue" Font-Bold
="true" />
<HeaderTemplate>
Name
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname"
runat ="server"
Text='<%#
Bind("name") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat
="server" Text='<%#
Bind("name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#3366CC"
ForeColor="#000099"
/>
<HeaderStyle BackColor="#9966FF"
/>
</asp:GridView>
<div>
</div>
</form>
</body>
</html>
//C# code
// Global Declaration
string strcon = "Data Source=FWSI-C25\\SqlExpress;Initial Catalog=Employee;Integrated Security=True";
SqlConnection con;
static DataSet ds;
SqlDataAdapter da;
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();
}
}
public void bind()
{
da = new SqlDataAdapter("select * from stu_info", strcon);
ds = new DataSet();
da.Fill(ds);
grd1.DataSource
= ds.Tables[0];
grd1.DataBind();
}
protected void
DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
grd1 .PageSize
= int.Parse(((DropDownList)sender).SelectedValue);
grd1.DataSource = ds.Tables[0];
grd1.DataBind();
}
protected void
grd1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd1.PageIndex
= e.NewPageIndex;
grd1.DataSource
= ds.Tables[0];
grd1.DataBind();
}
protected void
grd1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = new
DropDownList();
//adds variants of pager size
ddl.Items.Add("5");
ddl.Items.Add("10");
ddl.Items.Add("15");
ddl.Items.Add("20");
ddl.AutoPostBack = true;
//selects item due to the GridView current page size
ListItem li =
ddl.Items.FindByText(grd1.PageSize.ToString());
if (li != null)
ddl.SelectedIndex = ddl.Items.IndexOf(li);
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
//adds dropdownlist in the additional cell to the pager
table
Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
TableCell cell = new
TableCell();
cell.Style["padding-left"] = "60px";
cell.Style.Add("forecolor", "white");
cell.Controls.Add(new LiteralControl("Page
Size:"));
cell.ForeColor
= System.Drawing.Color.White;
cell.Controls.Add(ddl);
pagerTable.Rows[0].Cells.Add(cell);
}
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
//changes page size
grd1.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
//binds data source
grd1.DataSource =
ds.Tables[0];
grd1.DataBind();
}
0 comments:
Post a Comment