<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridview.aspx.cs" Inherits="_Default" %>
<!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>
<script type="text/javascript" language="javascript">
var message;
message = "<%=Message%>";
if (message != "") {
alert(message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td>
<asp:GridView ID="EmpGridView" DataKeyNames="EmpId" onrowdatabound="GridView1_RowDataBound" runat="server" OnRowCancelingEdit="EmpGridView_RowCancelingEdit"
AutoGenerateColumns="false" OnSorting="EmpGridView_Sorting" OnRowEditing="EmpGridView_RowEditing" OnRowDeleting="EmpGridView_RowDeleting" OnRowUpdating="EmpGridView_RowUpdating"
>
<Columns>
<asp:TemplateField HeaderText="EmployeeId">
<ItemTemplate>
<%# Eval("EmpId") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeName">
<ItemTemplate>
<%# Eval("EmployeeName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmp" Text='<%#Eval("EmployeeName") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DepartmentName">
<ItemTemplate>
<%# Eval("DepartmentName") %>
</ItemTemplate>
<EditItemTemplate>
<%-- <asp:TextBox ID="txtDept" Text='<%#Eval("DepartmentName") %>' runat="server" ></asp:TextBox>
--%> <asp:DropDownList ID="drpEmp" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Salary" HeaderText="Salary">
<ItemTemplate>
<%# Eval("Salary") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSal" Text='<%#Eval("Salary") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logo">
<ItemTemplate>
<%# Eval("Logo") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<%# Eval("Date") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<%# Eval("Active") %>
</ItemTemplate>
<EditItemTemplate >
<asp:CheckBoxList ID="CheckBoxList2" runat="server" s>
<asp:ListItem>Active</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Edit" ShowHeader ="false">
<ItemTemplate>
<asp:LinkButton ID="lnkedit" CommandName="Edit" runat="server">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server"
CommandName="Update" Text="Update" ></asp:LinkButton>
<%--<asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update" />--%>
<%-- <asp:Button ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" />--%>
<asp:LinkButton ID="btncancel" runat="server"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<span onclick = "retrun confirm('Are you sure you want to delete the record')">
<asp:LinkButton ID="lnkdelete" CommandName="Delete" runat="server">Delete</asp:LinkButton></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td></tr></table>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
public string Message;
public DataSet ds;
Class2 c2 = new Class2();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "";
LoadData("","");
}
}
protected void LoadData(string SortExp,string SortDir)
{
ds = c2.GetEmployees();
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
if (SortExp != string.Empty)
{
dv.Sort = string.Format("{0}{1}", SortExp, SortDir);
}
EmpGridView.DataSource = dv;
EmpGridView.DataBind();
}
protected void EmpGridView_Sorting(object sender, GridViewSortEventArgs e)
{
LoadData(e.SortExpression, SortOrder);
}
public string SortOrder
{
get
{
if (ViewState["SortOrder"].ToString() == "desc")
{
ViewState["SortOrder"]="asc";
}
else
{
ViewState["SortOrder"] = "desc";
}
return ViewState["SortOrder"].ToString();
}
set
{
ViewState["SortOrder"] = value;
}
}
protected void EmpGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
EmpGridView.EditIndex = -1;
LoadData("","");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp = (DropDownList)e.Row.FindControl("drpEmp");
DataTable dt = load_department();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][2].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[2].ToString();
CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
chkb.SelectedValue = drv[6].ToString();
}
}
}
protected void EmpGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
EmpGridView.EditIndex = e.NewEditIndex;
LoadData("","");
}
protected void EmpGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool Active;
int autoid = int.Parse(EmpGridView.DataKeys[e.RowIndex].Value.ToString());
TextBox tx1 = (TextBox)EmpGridView.Rows[e.RowIndex].FindControl("txtEmp");
DropDownList drp = (DropDownList)EmpGridView.Rows[e.RowIndex].FindControl("drpEmp");
TextBox tx3 = (TextBox)EmpGridView.Rows[e.RowIndex].FindControl("txtSal");
CheckBoxList chb = (CheckBoxList)EmpGridView.Rows[e.RowIndex].FindControl("CheckBoxList2");
int salary = int.Parse(tx3.Text);
if (chb.SelectedValue=="Active")
Active = true;
else
Active = false;
c2.UpdateEmployee(autoid, tx1.Text, drp.SelectedValue, salary, Active);
EmpGridView.EditIndex = -1;
LoadData("","");
Message = "Record Updated Successfully";
}
protected void EmpGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)EmpGridView.Rows[e.RowIndex];
int autoid = int.Parse(EmpGridView.DataKeys[e.RowIndex].Value.ToString());
c2.DeleteEmployee(autoid);
LoadData("","");
Message = "Record Deleted Successfully";
}
public DataTable load_department()
{
DataTable dt = new DataTable();
ds = c2.GetDepartment();
dt = ds.Tables["dept"];
return dt;
}
}
No comments:
Post a Comment