Description:-
In this Example we
Explain that How to Create a Paging in Gridview. We also provide Paging
Facility with Insert,upadte,Delete operation in Gridview.
What is Paging:-
Paging means there are
number of pages in which every page contain a bunch of record to Navigate the
Record.
For example:-
If you have a thousand
of Record and you want to Display in Gridview at that time our screen can not
capable of Display all record properly so at that time we can partition this
all record in Different pages with a Bunch of Record using Paging Facility
Provided by Gridview in Asp.Net so we can easily Handle and Display it.
there are many types of paging mode styles are available in gridview control for paging those are
- Numeric
- NextPrevious
- NextPreviousFirstLast
- NumericFirstLast
Before use these options in our Gridview control we
first need to set AllowPaging="true" and PageSize="5" Properties here pageSize you can
change based on your requirement after set these properties we can use
different types of pagersetting modes to show different type of paging for
gridview control.
NumericFirstLast: By using this mode in gridview paging will Display First
and Last buttons.
Numeric: By default Gridview
will set this numeric paging once we set AllowPaging
and PageSize properties in gridview
control. to set this property to our gridview we have to set the pagersetting property like this
<PagerSettings
Mode="Numeric"
PageButtonCount="4"
/>
NextPrevious: by using this mode
the paging in gridview will display Next and Previous Buttons. To set this mode
we need to change the PagerSettings mode
like this
<PagerSettings
Mode="NextPrevious"
PageButtonCount="4"
PreviousPageText="Previous"
NextPageText="Next"
/>
NextPreviousFirstLast: : by
using this mode the paging in gridview will display Next and Previous Buttons and First and Last Buttons. To set
this mode we need to change the PagerSettings
mode like this
<PagerSettings
Mode="NextPreviousFirstLast"
PageButtonCount="4"
PreviousPageText="Previous"
NextPageText="Next"
FirstPageText="First"
LastPageText="Last"
/>
To Craete this First you have to Create a PageIndexChanging Event of
Gridview like as follows:
onpageindexchanging="GridView1_PageIndexChanging"
and Define the Code inside this Event like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
grid();
}
to show Example of Paging in DataList Paging or Custom Paging in DataListTo Show Example of Create Paging in Reapeter Control plz Click Here Paging in Reapeter Control
bill.aspx:-
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="bill.aspx.cs" Inherits="bill" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function
confirmSubmit() {
var
agree = confirm("Do you really want to Delete
Record?");
if
(agree)
return true;
else
return false;
}
</script>
<asp:GridView ID="GridView1" runat="server"
ForeColor="#333333" GridLines="None"
DataKeyNames="id" AutoGenerateColumns="False"
AllowPaging="true" PageSize="2"
OnRowDeleting="DeleteRecord"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdatabound="GridView1_RowDataBound1" ShowFooter="True"
onpageindexchanging="GridView1_PageIndexChanging"
>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2ff1BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="80" Height="80" ImageUrl='<%#
Eval("image") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product
Name" SortExpression="pnm">
<ItemTemplate>
<%#
Eval("pnm") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbl" runat="server" Text="page
Total" ></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Grand
Total" ></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Prize" SortExpression="prize">
<ItemTemplate>
<%#
Eval("prize") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="quantity">
<ItemTemplate>
<%#
Eval("quantity") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtqu" runat="Server" Text='<%#
Eval("quantity") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="grand" runat="server" Text=' <%#
Eval("total") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblrn" runat="server" ></asp:Label><br />
<asp:Label ID="lblgrand" runat="server" ></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" ImageUrl="~/delete.png" runat="server" Width="50" Height="50" CommandName="Delete" ToolTip="Delete
Record" OnClientClick="return
confirmSubmit()"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton3" ImageUrl="~/ed.jpeg" CommandName="Edit" Width="50" Height="50" runat="server" ToolTip= "Update
Record" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" CommandName="update" />
<asp:Button ID="Button2" runat="server" Text="cancel" CommandName="cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>
</asp:Content>
bill.aspx.cs:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;
public partial class bill :
System.Web.UI.Page
{
int
irn;
protected void
Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
grid();
}
}
public void
grid()
{
string
con = @"Data Source=SQLDB;Initial Catalog=Demo;User
ID=Demoh;Password=Demo1@";
//Menu inm =
(Menu)this.Master.FindControl("Menu1");
string q1
= "select * from manage";
//String q2 = "select
sum(total) from manage";
DataSet
dsOrders = new DataSet();
SqlDataAdapter da
= new SqlDataAdapter();
SqlConnection cn
= new SqlConnection(con);
SqlCommand
cmd = new SqlCommand(q1,
cn);
// SqlCommand cmd1 = new
SqlCommand(q2, cn);
cn.Open();
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
da.Fill(dsOrders);
GridView1.DataSource = dsOrders;
GridView1.DataBind();
// igr =
Convert.ToInt32(dsOrders.Tables[0].AsEnumerable().Sum(x =>
x.Field<int>("total")));
}
public string
to()
{
string
con = @"Data Source=SQLDB;Initial Catalog=Demo;User
ID=Demoh;Password=Demo1@";
String q2
= "select sum(total) from manage";
SqlConnection cn
= new SqlConnection(con);
SqlCommand
cmd1 = new SqlCommand(q2,
cn);
cn.Open();
string
total = cmd1.ExecuteScalar().ToString();
return
total;
}
protected void
DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
int id
= Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
string
con = @"Data Source=SQLDB;Initial Catalog=Demo;User
ID=Demoh;Password=Demo1@";
//Menu inm =
(Menu)this.Master.FindControl("Menu1");
string q1
= "delete from manage where id = "+id;
SqlConnection cn
= new SqlConnection(con);
SqlCommand
cmd = new SqlCommand(q1,
cn);
cn.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
grid();
}
protected void
GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id
= Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow
row = GridView1.Rows[e.RowIndex];
TextBox qu
= (TextBox)row.FindControl("txtqu");
string
con = @"Data Source=SQLDB;Initial Catalog=Demo;User
ID=Demoh;Password=Demo1@";
if
(qu.Text.Length > 0)
{
string q1
= "update manage set
quantity = '" + qu.Text+ "'
where id = '" + id + "'";
SqlConnection cn
= new SqlConnection(con);
SqlCommand
cmd = new SqlCommand(q1,
cn);
cn.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
grid();
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(),
"key", "<script>
alert('plz fill the value');</script>");
qu.Focus();
}
}
protected void
GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grid();
}
protected void
GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
grid();
}
protected void
GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
//Label lblGTUnitInStock =
(Label)e.Row.FindControl("lblgrandtotal");
int a
= Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
"total"));
// Label lblGTUnitInStock =
(Label)e.Row.FindControl("grand");
irn+=a;
}
if
(e.Row.RowType == DataControlRowType.Footer)
{
Label rn
= (Label)e.Row.FindControl("lblrn");
Label gr
= (Label)e.Row.FindControl("lblgrand");
rn.Text = irn.ToString();
gr.Text = to();
}
}
protected void
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
grid();
}
}
0 comments:
Post a Comment