Grid Editing ,Deleting,Updating...
SOURCE
<asp:GridView ID="grdDemo2"
runat="server"
AutoGenerateColumns="False"
CellPadding="4"
EnableModelValidation="True"
ForeColor="#333333"
GridLines="None"
OnPageIndexChanging="grdDemo_PageIndexChanging" OnRowCancelingEdit="grdDemo_RowCancelingEdit"
OnRowEditing="grdDemo_RowEditing"
OnRowUpdating="grdDemo_RowUpdating"
Style="font-weight: 700">
<AlternatingRowStyle BackColor="#FFCC66" ForeColor="White" Font-Names="Arial" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblId" runat="server"
Text='<%#
Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%#
Eval("Name") %>'>></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB">
<ItemTemplate>
<%# Eval("Date") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDob" runat="server"
Text='<%#
Eval("Date") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="EDIT"
ShowEditButton="True"
ShowHeader="True"
/> //This is editing column
</Columns>
</asp:GridView>
NOTE:
<%# Eval("Id")%> : Id is a variable in the class file "Utility"
<%# Eval("Name")%> Name is a variable in the class file "Utility" <%#Eval("Email")%> Email is a variable in the class file "Utility"
<%# Eval("DOB")%> DOB is a variable in the class file "Utility"
C# Code
- Create an asp.net app
- Add a webform "AddEmployee.aspx"
- Add a class file "HelperCls.cs"
HelperCls.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;
using
System.Data.SqlClient;
namespace Demo_Pjct
{
public class HelperCls
{
SqlConnection
con = new SqlConnection("Data Source=10.18.184.133;Initial
Catalog=ABC_Demo;User ID=niituser;Password=niituser");
private
string name;
private
Int32 id;
private
string date;
public Int32 Id
{
get
{ return this.id;
}
set
{ this.id = value;
}
}
public string Name
{
get
{ return this.name;
}
set
{ this.name = value;
}
}
public string Date
{
get
{ return this.date;
}
set
{ this.date = value;
}
}
public List<HelperCls>
getAll(string str)
{
List<HelperCls> lst = new
List<HelperCls>();
HelperCls
obj;
try
{
con.Open();
SqlCommand cmd = new
SqlCommand(str, con);
SqlDataReader
reader = cmd.ExecuteReader();
while
(rder.Read())
{
obj = new HelperCls();
obj.Id = Convert.ToInt32(rder["id"]);
obj.Name = Convert.ToString(rder["name"]);
obj.Date = Convert.ToString(rder["dob"]);
lst.Add(obj);
}
}
catch
(Exception)
{
}
finally
{
con.Close();
con.Dispose();
}
return
lst;
}
public bool Update(string
str)
{
bool
status = false;
try
{
con.Open();
SqlCommand cmd = new
SqlCommand(str, con);
Int32
c = cmd.ExecuteNonQuery();
if
(c == 1)
{
status = true;
}
}
catch
(Exception)
{
}
finally
{
con.Close();
con.Dispose();
}
return
true;
}
}
}
AddEmployee.aspx
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
Employee_Managment_System
{
public
partial
class
WebForm1
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(!IsPostBack)
{
Fill_Grid();
}
public void
Fill_Grid()
{
string str = "select * from tbl_employee";
string str = "select * from tbl_employee";
List<HelperCls>
Emp_List = emp.getAll(str );
if
(Emp_List.Count > 0)
{
grdEmployee.DataSource =
Emp_List;
grdEmployee.DataBind();
}
else
{
empCount = 1;
}
}
protected void grdEmployee_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
grdEmployee.PageIndex = e.NewPageIndex;
Fill_Grid();
}
protected
void grdEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
e.Cancel = true;
grdEmployee.EditIndex = -1;
Fill_Grid();
}
protected
void grdEmployee_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
HelperCls helper= new HelperCls();
HelperCls helper= new HelperCls();
Label
lblId = (Label)grdDemo.Rows[e.RowIndex].FindControl("lblId");
TextBox
txtName = (TextBox)grdDemo.Rows[e.RowIndex].FindControl("txtName");
TextBox
txtDOB = (TextBox)grdDemo.Rows[e.RowIndex].FindControl("txtDob");
Int32
id =Convert.ToInt32(lblId.Text);
string
name = txtName.Text;
DateTime
dt =Convert.ToDateTime (txtDOB.Text);
string
str ="update tbl_employee set name='"+name+"',dob='"+dt+"'
where id='"+id+"'";
bool
status = helper.Update(str);
cmd = new SqlCommand(str, con);
Int32 status = cmd.ExecuteNonQuery();
Int32 status = cmd.ExecuteNonQuery();
if
(status == 1)
{
Response.Write("<script>alert('Updated')</script>");
grdDemo.EditIndex = -1;
ShowDemoGrid();
}
else
{
Response.Write("<script>alert('Updation
faild')</script>");
}
}
}
}
No comments:
Post a Comment