Create an example of GridView Control to bind employee data and displays in a tabular layout by specifying the columns as BoundField, CommandField and performing the editing, deleting, sorting, & paging operations.
GridViewExample1.aspx
GridViewExample1.aspx.cs
Edit:
GridViewExample1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewExample1.aspx.cs" Inherits="GridViewExample1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<h1 style="color: darkblue">Welcome to GridView
Examaple</h1>
<div>
<asp:GridView ID="Gridview1" runat="server" PageSize="5" AutoGenerateColumns="False"
DataKeyNames="EmpId" AllowSorting="True" AllowPaging="True" BorderWidth="1px" CellPadding="4" OnRowEditing="Gridview1_RowEditing" OnRowCancelingEdit="Gridview1_RowCancelingEdit" OnRowUpdating="Gridview1_RowUpdating" OnRowDeleting="Gridview1_RowDeleting" OnPageIndexChanging="Gridview1_PageIndexChanging" OnSorting="Gridview1_Sorting" BackColor="White" BorderColor="#3366CC" BorderStyle="None">
<Columns>
<asp:CommandField ShowEditButton="true" HeaderText="EditOpration" ItemStyle-Width="3%">
<ItemStyle Width="3%"></ItemStyle>
</asp:CommandField>
<asp:CommandField ShowDeleteButton="true" HeaderText="DeleteOpration" ItemStyle-Width="5%">
<ItemStyle Width="5%"></ItemStyle>
</asp:CommandField>
<asp:BoundField HeaderText="Emp Id" DataField="EmpId" ReadOnly="true" SortExpression="EmpId" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Emp Name" DataField="EmpName" SortExpression="EmpName" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Address" DataField="EmpAddress" SortExpression="EmpAddress" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Salary" DataField="EmpSalary" SortExpression="EmpSalary" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="EmailId" DataField="EmpEmail" SortExpression="EmpEmail" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="EmpPhone" DataField="EmpPhone" SortExpression="EmpPhone" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="EmpPine" DataField="EmpPine" SortExpression="EmpPine" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="DeptId" DataField="DeptId" SortExpression="DeptId" ItemStyle-Width="12%">
<ItemStyle Width="12%"></ItemStyle>
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
</div>
<div align="center">
<asp:Label ID="lblDisplay" runat="server"></asp:Label></div>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class GridViewExample1 : System.Web.UI.Page
{
SqlConnection con = null;
SqlDataAdapter da = null;
SqlCommand cmd = null;
DataSet ds = null;
string strSql = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
if (!Page.IsPostBack)
{
ViewState["SortOn"] = "EmpId";
ViewState["SortBy"] = "Asc";
BindEmpData();
}
}
void BindEmpData()
{
strSql = "Select * from Employee Order By " + ViewState["SortOn"].ToString() + " " + ViewState["SortBy"].ToString();
da = new SqlDataAdapter(strSql, con);
ds = new DataSet();
da.Fill(ds, "Employee");
Gridview1.DataSource = ds;
Gridview1.DataBind();
}
protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
Gridview1.EditIndex = e.NewEditIndex;
BindEmpData();
}
protected void
Gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Gridview1.EditIndex = -1;
BindEmpData();
}
protected void
Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string empId = e.Keys["EmpId"].ToString();
string empName = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
string empAddress = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
string empSalary = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
string empEmail = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
string empPhone = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();
string empPine = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.Trim();
string deptId = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.Trim();
strSql = "Update Employee set EmpName='" + empName + "',EmpAddress='" + empAddress + "',EmpSalary=" + empSalary + ",EmpEmail='" + empEmail + "',EmpPhone='" + empPhone + "',EmpPine='" + empPine + "',DeptId=" + deptId + "Where
EmpId=" + empId;
if (con.State != ConnectionState.Open)
con.Open();
cmd = new SqlCommand(strSql, con);
if (cmd.ExecuteNonQuery() > 0)
{
lblDisplay.Text = "<b
style='color:green'>Employee Information Updation hase been
successfull</b>";
Gridview1.EditIndex = -1;
BindEmpData();
}
else
{
lblDisplay.Text = "<b
style='color:red'>Employee Informetion updation has been
faild</b>";
}
}
protected void
Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string empId = e.Keys["EmpId"].ToString();
strSql = "Delete from Employee where EmpId=" + empId;
if (con.State != ConnectionState.Open)
con.Open();
cmd = new SqlCommand(strSql, con);
if
(cmd.ExecuteNonQuery() > 0)
{
lblDisplay.Text = "<b
style='color:green'>Employee Information Deletaion hase been
successfull</b>";
BindEmpData();
}
else
{
lblDisplay.Text = "<b
style='color:red'>Employee Information Delation hase been
faild</b>";
}
con.Close();
}
protected void
Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Gridview1.PageIndex = e.NewPageIndex;
BindEmpData();
}
protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortOn"] = e.SortExpression;
if (ViewState["SortBy"].ToString() == "Asc")
{
ViewState["SortBy"] = "Desc";
}
else
{
ViewState["SortBy"] = "Asc";
}
BindEmpData();
}
}
Output:Edit:
No comments:
Post a Comment