In this article I will explain How to edit, update and
delete data from xml through gridview. I will bind gridview to xml file, then
update and delete data from it.
1. Add xml file in application and add some data
to it.
2. Add webform to
application and add gridview on webform.
3. Add template
fields and edit update and delete command fields.
4. Add required
events to gridview ex: Rowdeleting, RowCancelingEdit, Rowupdating etc.
5. Now bind gridview
with xml file.
6. Now using given
code Update and delete data.
Gridview Design:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
<asp:TextBox ID="txtDept" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="txtSalary" runat="server" Text='<%# Bind("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Asp.NET code using
C#:
public partial class XMLGRID : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGrid();
}
}
public void bindGrid()
{
XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("XMLFile.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void
GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bindGrid();
DataSet ds = GridView1.DataSource as DataSet;
ds.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("XMLFile.xml"));
bindGrid();
}
protected void
GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindGrid();
}
protected void
GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id =
GridView1.Rows[e.RowIndex].DataItemIndex;
String Name =
(GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text;
String Age = (GridView1.Rows[e.RowIndex].FindControl("txtAge") as TextBox).Text;
String Department =
(GridView1.Rows[e.RowIndex].FindControl("txtDept") as TextBox).Text;
String Salary =
(GridView1.Rows[e.RowIndex].FindControl("txtSalary") as TextBox).Text;
GridView1.EditIndex = -1;
bindGrid();
DataSet ds = GridView1.DataSource as DataSet;
ds.Tables[0].Rows[id]["Name"] = Name;
ds.Tables[0].Rows[id]["Age"] = Age;
ds.Tables[0].Rows[id]["Department"] = Department;
ds.Tables[0].Rows[id]["Salary"] = Salary;
ds.WriteXml(Server.MapPath("XMLFile.xml"));
bindGrid();
}
}
0 comments:
Post a Comment