Insert a Xml File in your Project
Then give the below query in xml page
Type below query In your aspx Page
Then give the below query in xml page
<Employees>
<Employee>
<ID>2</ID>
<name>Durai</name>
<age>25</age>
<sex>male</sex>
</Employee>
<Employee>
<ID>3</ID>
<name>ras</name>
<age>23</age>
<sex>female</sex>
</Employee>
</Employees>
Then create a EmployeeInfo.cs in your Project
And give the below query in EmployeeInfo.cs Page.
namespace EditableGridView.Class
{
public class EmployeeInfo
{
public
EmployeeInfo()
{
//
// TODO:
Add constructor logic here
//
}
private
Int64 _ID;
public Int64 ID
{
get
{ return _ID; }
set
{ _ID = value; }
}
private
string _name;
public string name
{
get
{ return _name; }
set
{ _name = value; }
}
private
Int32 _age;
public Int32 age
{
get
{ return _age; }
set
{ _age = value; }
}
private
string _sex;
public string sex
{
get
{ return _sex; }
set
{ _sex = value; }
}
}
}
Type below query In your aspx Page
<asp:GridView ID="gvwExampleXML" runat="server" AutoGenerateColumns="false" Width="100%"ShowFooter="true"onrowcancelingedit="gvwExampleXML_RowCancelingEdit" DataKeyNames="ID"
onrowcommand="gvwExampleXML_RowCommand"
onrowdatabound="gvwExampleXML_RowDataBound"
onrowdeleting="gvwExampleXML_RowDeleting"
onrowediting="gvwExampleXML_RowEditing"
onrowupdating="gvwExampleXML_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate >
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtName"
ValidationGroup="Update" runat="server" ErrorMessage="Enter Your Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("name") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName1" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" ControlToValidate="txtName1"
ValidationGroup="Insert" runat="server" ErrorMessage="Enter Your Name."></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%#Eval("age") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" ControlToValidate="txtAge"
ValidationGroup="Update" runat="server" ErrorMessage="Enter Your Age."></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("age") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAge1" runat="server" Text='<%#Eval("age") %>'></asp:TextBox>
asp:RequiredFieldValidator ID="RequiredFieldValidator11" ControlToValidate="txtAge1"ValidationGroup="Insert"
runat="server"
ErrorMessage="Enter
Your Age."></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:DropDownList ID="ddlSex" runat="server">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%#
Eval("sex")%>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSex1" runat="server">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" >
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True" CommandName="Update"Text="Update" OnClientClick="return confirm('Update?')" ValidationGroup="Update"></asp:LinkButton>
<asp:ValidationSummary ID="vsUpdate" runat="server" ShowMessageBox="true" ShowSummary="false"ValidationGroup="Update" Enabled="true" HeaderText="Validation Summary..." />
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="True" CommandName="Insert"ValidationGroup="Insert" Text="Insert"></asp:LinkButton>
<asp:ValidationSummary ID="vsInsert" runat="server" ShowMessageBox="true" ShowSummary="false"ValidationGroup="Insert" Enabled="true" HeaderText="Validation..." />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"Text="Edit"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" CommandName="Delete"Text="Delete" OnClientClick="return confirm('Delete?')"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Then Give the below query in aspx.cs Page
Add using EditableGridView.Class; Namespace And give the query
Insert
protected void
gvwExampleXML_RowCommand(object sender, GridViewCommandEventArgs e)
{
if
(e.CommandName.Equals("Insert"))
{
EmployeeInfo
einfo = new EmployeeInfo();
einfo.name = ((TextBox)gvwExampleXML.FooterRow.FindControl("txtName1")).Text;
einfo.age = Convert.ToInt32(((TextBox)gvwExampleXML.FooterRow.FindControl("txtAge1")).Text);
einfo.sex = ((DropDownList)gvwExampleXML.FooterRow.FindControl("ddlSex1")).SelectedValue;
new
mainXML().insertEmployeeInfo(einfo);
gvwExampleXML.EditIndex = -1;
bindEmployeeGrid();
}
}
Then Create a cs(mainXML.cs) file in your project And Give the query
public List<EmployeeInfo> getEmployeeList()
{
List<EmployeeInfo> eList = new
List<EmployeeInfo>();
EmployeeInfo
objEmp = null;
DataSet
ds = new DataSet();
ds.ReadXml(xmlConString);
if
(ds.Tables.Count != 0)
{
for
(int i = 0; i < ds.Tables[0].Rows.Count;
i++)
{
objEmp = new EmployeeInfo();
objEmp.ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]);
objEmp.name = Convert.ToString(ds.Tables[0].Rows[i]["name"]);
objEmp.age = Convert.ToInt32(ds.Tables[0].Rows[i]["age"]);
objEmp.sex = Convert.ToString(ds.Tables[0].Rows[i]["sex"]);
eList.Add(objEmp);
}
}
return
eList;
}
public Boolean
insertEmployeeInfo(EmployeeInfo eInfo)
{
DataSet
ds = new DataSet();
ds.ReadXml(xmlConString);
DataRow
dr = ds.Tables[0].NewRow();
dr[0] = eInfo.ID;
dr[1] = eInfo.name;
dr[2] = eInfo.age;
dr[3] = eInfo.sex;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(xmlConString);
return true;
}
Edit
in Aspx.Cs Page Give the below query
protected void
gvwExampleXML_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwExampleXML.EditIndex =
e.NewEditIndex;
bindEmployeeGrid();
}
Update
in Aspx.Cs Page Give the below query
protected void
gvwExampleXML_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
EmployeeInfo
einfo = new EmployeeInfo();
einfo.ID = Convert.ToInt64(gvwExampleXML.DataKeys[e.RowIndex].Values[0].ToString());
einfo.name = ((TextBox)gvwExampleXML.Rows[e.RowIndex].FindControl("txtName")).Text;
einfo.age =Convert.ToInt32(((TextBox)gvwExampleXML.Rows[e.RowIndex].FindControl("txtAge")).Text);
einfo.sex = ((DropDownList)gvwExampleXML.Rows[e.RowIndex].FindControl("ddlSex")).SelectedValue;
new mainXML().updateEmployeeInfo(einfo);
gvwExampleXML.EditIndex = -1;
bindEmployeeGrid();
}
in mainXML.cs Give the below query
public Boolean
updateEmployeeInfo(EmployeeInfo eInfo)
{
DataSet
ds = new DataSet();
ds.ReadXml(xmlConString);
if
(ds.Tables.Count != 0)
{
for
(int i = 0; i < ds.Tables[0].Rows.Count;
i++)
{
if
(Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]) == eInfo.ID)
{
ds.Tables[0].Rows[i]["name"] = eInfo.name;
ds.Tables[0].Rows[i]["age"] = eInfo.age;
ds.Tables[0].Rows[i]["sex"] = eInfo.sex;
ds.AcceptChanges();
ds.WriteXml(xmlConString);
break;
}
}
}
//XML Updated
return true;
}
Delete
in Aspx.Cs Page Give the below query
protected void
gvwExampleXML_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id
= Convert.ToInt32(gvwExampleXML.DataKeys[e.RowIndex].Values[0].ToString());
new mainXML().deleteEmployeeInfo(id);
bindEmployeeGrid();
}
in mainXML.cs Give the below query
public Boolean
deleteEmployeeInfo(int id)
{
DataSet
ds = new DataSet();
ds.ReadXml(xmlConString);
if
(ds.Tables.Count != 0)
{
for
(int i = 0; i < ds.Tables[0].Rows.Count;
i++)
{
if
(Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]) == id)
{
ds.Tables[0].Rows[i].Delete();
ds.AcceptChanges();
ds.WriteXml(xmlConString);
break;
}
}
}
//XML Delete
return true;
}
Then Run your Project
The Output is Display same as given below
Thank You
No comments:
Post a Comment