Create
a page with name of GridView.aspx
<div style="text-align:center;">
<div>
Employee Name
<asp:TextBox ID="txtEmpName"
runat="server">asp:TextBox> <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
<asp:LinkButton ID="lnkFirst" runat="server" Text="First Record" OnClick="lnkFirst_Click">asp:LinkButton>
Go to Page <asp:DropDownList ID="ddlJumpToPage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlJumpToPage_SelectedIndexChanged">asp:DropDownList>
<asp:LinkButton ID="lnkLast"
runat="server"
Text="Last
Record" OnClick="lnkLast_Click">asp:LinkButton>
div>
<asp:GridView ID="gvCustDetails" CssClass="Grid" runat="server"
DataKeyNames="empID"
AutoGenerateColumns="False"
Width="80%"
HorizontalAlign="Center"
PageSize="25">
<HeaderStyle CssClass="GridHeader">HeaderStyle>
<AlternatingRowStyle CssClass="GridAtlItem"/>
<Columns>
<asp:TemplateField HeaderText="Sr." >
<ItemTemplate>
<%# ((GridViewRow)Container).RowIndex + 1%>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lblTicketNo" runat="server" Text='<%#Eval("empName")
%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblSal" runat="server" Text='<%#Eval("Salary")
%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:ButtonField CausesValidation="True" CommandName="Delete" Text="Delete" />
<asp:ButtonField CausesValidation="True" CommandName="Edit" Text="Edit" />
Columns>
asp:GridView>
<asp:LinkButton ID="lnkAdd" runat="server" Text="Add" OnClick="lnkAdd_Click">asp:LinkButton>
div>
Write
below code in GridView.aspx.cs page
if
(!IsPostBack)
{
int
TotalRows = this.BindGrid(1);
this.populateList(TotalRows);
}
private int BindGrid(int
CurrentPageNo)
{
int
TotalRows = 0;
SqlParameter
[] pArray = new SqlParameter[3];
pArray[0] =
new SqlParameter("@intPageSize", SqlDbType.Int);
pArray[0].Value = 25;
pArray[1] = new
SqlParameter("@intCurrentPage",
SqlDbType.Int);
pArray[1].Value = CurrentPageNo;
pArray[2] = new
SqlParameter("@EmpName",
SqlDbType.VarChar,200);
pArray[2].Value =
txtEmpName.Text.Trim().Replace("'","''");
pArray[3] = new
SqlParameter("@intTotalRecords",
SqlDbType.Int);
pArray[3].Direction = ParameterDirection.Output;
DataTable
dt = objDAL.getDataTable("usp_EmployeeDetails_InGV",pArray);
TotalRows = Convert.ToInt32(pArray[3].Value); gvCustDetails.PageIndex = CurrentPageNo
- 1;
gvCustDetails.DataSource = dt;
gvCustDetails.DataBind();
return
TotalRows;
}
private void populateList(int
TotalRows)
{
int
PageCount = this.CalculateTotalPages(TotalRows);
for (int i = 1; i <= PageCount; i++)
{
ddlJumpToPage.Items.Add(new ListItem(i.ToString(),
i.ToString()));
}
}
private int CalculateTotalPages(int
intTotalRows)
{
int
intPageCount = 1;
double
dblPageCount = (double)(Convert.ToDecimal(intTotalRows)
/ Convert.ToDecimal(gvCustDetails.PageSize));
intPageCount = Convert.ToInt32(Math.Ceiling(dblPageCount));
return
intPageCount;
}
protected void PageNumberChanged()
{
int
PageNo = Convert.ToInt32(ddlJumpToPage.SelectedItem.Value);
this.BindGrid(PageNo);
}
protected void ddlJumpToPage_SelectedIndexChanged(object sender, EventArgs
e)
{
PageNumberChanged();
}
protected void lnkFirst_Click(object
sender, EventArgs e)
{
this.BindGrid(1);
ddlJumpToPage.SelectedIndex = 0;
}
protected void lnkLast_Click(object
sender, EventArgs e)
{
this.BindGrid(ddlJumpToPage.Items.Count);
ddlJumpToPage.SelectedIndex =
ddlJumpToPage.Items.Count - 1;
}
protected void btnSearch_Click(object
sender, EventArgs e)
{
ddlJumpToPage.Items.Clear();
int
TotalRows = this.BindGrid(1);
this.populateList(TotalRows);
}
Create a procedure for filter purpose and create
EmployeeMaster table with the given fields and datatype
ALTER procedure usp_EmployeeDetails_InGV
(
@intPageSize int,
@intCurrentPage int,
@empName varchar(100),
@intTotalRecords int
output
)
AS
BEGIN
DECLARE
@EmployeeMaster TABLE(
empId int IDENTITY not null, --to insert
identity value start from 1
empName varchar(50) ,
salary varchar(100),
UserId varchar(100)
)
DECLARE @start int
DECLARE @STR as VARCHAR(400)
SET @start=@intPageSize*(@intCurrentPage
- 1)
INSERT INTO @EmployeeMaster (empName, salary, UserId)
--Create a table name EmployeeMaster with following fields
SELECT empName, salary, UserId
FROM
EmployeeMaster WHERE empName like @empName+'%'
SELECT
@intTotalRecords=COUNT(*)
FROM
EmployeeMaster WHERE empName like @empName+'%'
--SET @STR = 'SELECT
empName, salary, UserId
-- FROM EmployeeMaster WHERE empName like
''%'+@empName+'%'''
--PRINT (@STR)
SET ROWCOUNT @intPageSize
SELECT empId, empName, salary, UserId FROM @EmployeeMaster WHERE
empId > @start
SET ROWCOUNT 0
END
Comments