GridView XML Edit Delete Insert Update

GridView XML Edit Delete Insert Update. In this example i'm explaining how to Edit Delete Insert and update XML file data in GridView using C# and VB.Net in Asp.Net.

GridView Xml Edit Delete Insert Update Asp.Net
I have created a XML file with three fields Firstname,LastName and Location.

XML file looks like mentioned below.

01<!--xml version="1.0" standalone="yes"?-->
02<employees>
03  <details>
04    <firstname>Amit</firstname>
05    <lastname>Jain</lastname>
06    <location>Mumbai</location>
07  </details>
08  <details>
09    <firstname>User</firstname>
10    <lastname>3</lastname>
11    <location>Noida</location>
12  </details>
13  <details>
14    <firstname>User</firstname>
15    <lastname>4</lastname>
16    <location>Bangalore</location>
17  </details>
18</employees>

I have placed one gridview on aspx page and three textbox in editItemTemplate of gridview for editing, one button and three textbox in Footer Template for Inserting new records in XML file.

HTML SOURCE OF PAGE
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" ShowFooter="True" 
              onrowcancelingedit="GridView1_RowCancelingEdit" 
              onrowdeleting="GridView1_RowDeleting" 
              onrowediting="GridView1_RowEditing" 
              onrowupdating="GridView1_RowUpdating" 
              onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
 
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btn" runat="server" Text="Insert" CommandName="InsertXMLData"/>
<asp:TextBox ID="txtFirstNameInsert" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
 
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("LastName") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastNameInsert" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
 
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" Text='<%#Bind("Location") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLocationInsert" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


c# CODE
01protected void Page_Load(object sender, EventArgs e)
02    {
03        if (!Page.IsPostBack)
04            BindGridView();
05    }
06    protected void BindGridView()
07    {
08        DataSet dsXML = new DataSet();
09        dsXML.ReadXml(Server.MapPath("~/App_Data/XMLFile.xml"));
10        GridView1.DataSource = dsXML;
11        GridView1.DataBind();
12        GridView1.ShowFooter = true;
13    }
14 
15    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
16    {
17        BindGridView();
18        DataSet dsGrid = (DataSet)GridView1.DataSource;
19        dsGrid.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
20        dsGrid.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"));
21        BindGridView();
22 
23    }
24    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
25    {
26        GridView1.ShowFooter = false;
27        GridView1.EditIndex = e.NewEditIndex;
28        BindGridView();
29    }
30    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
31    {
32        int index = GridView1.Rows[e.RowIndex].DataItemIndex;
33        string firstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
34        string lastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
35        string location = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLocation")).Text;
36        GridView1.EditIndex = -1;
37        BindGridView();
38        DataSet dsUpdate = (DataSet)GridView1.DataSource;
39        dsUpdate.Tables[0].Rows[index]["FirstName"] = firstName;
40        dsUpdate.Tables[0].Rows[index]["LastName"] = lastName;
41        dsUpdate.Tables[0].Rows[index]["Location"] = location;
42        dsUpdate.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"));
43        BindGridView();
44 
45    }
46    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
47    {
48        GridView1.EditIndex = -1;
49        BindGridView();
50    }
51 
52    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
53    {
54 
55        if(e.CommandName == "InsertXMLData")
56        {
57        string firstname = ((TextBox)GridView1.FooterRow.FindControl("txtFirstNameInsert")).Text;
58        string lastname = ((TextBox)GridView1.FooterRow.FindControl("txtLastNameInsert")).Text;
59        string location = ((TextBox)GridView1.FooterRow.FindControl("txtLocationInsert")).Text;
60        BindGridView();
61        DataSet dsInsert = (DataSet)GridView1.DataSource;
62        DataRow drInsert = dsInsert.Tables[0].NewRow();
63        drInsert["FirstName"] = firstname;
64        drInsert["LastName"] = lastname;
65        drInsert["Location"] = location;
66        dsInsert.Tables[0].Rows.Add(drInsert);
67        dsInsert.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"));
68        BindGridView();
69        }
70    }

VB.NET
01Protected Sub Page_Load(sender As Object, e As EventArgs)
02 If Not Page.IsPostBack Then
03  BindGridView()
04 End If
05End Sub
06Protected Sub BindGridView()
07 Dim dsXML As New DataSet()
08 dsXML.ReadXml(Server.MapPath("~/App_Data/XMLFile.xml"))
09 GridView1.DataSource = dsXML
10 GridView1.DataBind()
11 GridView1.ShowFooter = True
12End Sub
13 
14Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
15 BindGridView()
16 Dim dsGrid As DataSet = DirectCast(GridView1.DataSource, DataSet)
17 dsGrid.Tables(0).Rows(GridView1.Rows(e.RowIndex).DataItemIndex).Delete()
18 dsGrid.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"))
19 BindGridView()
20 
21End Sub
22Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
23 GridView1.ShowFooter = False
24 GridView1.EditIndex = e.NewEditIndex
25 BindGridView()
26End Sub
27Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
28 Dim index As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
29 Dim firstName As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtFirstName"), TextBox).Text
30 Dim lastName As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtLastName"), TextBox).Text
31 Dim location As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtLocation"), TextBox).Text
32 GridView1.EditIndex = -1
33 BindGridView()
34 Dim dsUpdate As DataSet = DirectCast(GridView1.DataSource, DataSet)
35 dsUpdate.Tables(0).Rows(index)("FirstName") = firstName
36 dsUpdate.Tables(0).Rows(index)("LastName") = lastName
37 dsUpdate.Tables(0).Rows(index)("Location") = location
38 dsUpdate.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"))
39 BindGridView()
40 
41End Sub
42Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
43 GridView1.EditIndex = -1
44 BindGridView()
45End Sub
46 
47Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
48 
49 If e.CommandName = "InsertXMLData" Then
50  Dim firstname As String = DirectCast(GridView1.FooterRow.FindControl("txtFirstNameInsert"), TextBox).Text
51  Dim lastname As String = DirectCast(GridView1.FooterRow.FindControl("txtLastNameInsert"), TextBox).Text
52  Dim location As String = DirectCast(GridView1.FooterRow.FindControl("txtLocationInsert"), TextBox).Text
53  BindGridView()
54  Dim dsInsert As DataSet = DirectCast(GridView1.DataSource, DataSet)
55  Dim drInsert As DataRow = dsInsert.Tables(0).NewRow()
56  drInsert("FirstName") = firstname
57  drInsert("LastName") = lastname
58  drInsert("Location") = location
59  dsInsert.Tables(0).Rows.Add(drInsert)
60  dsInsert.WriteXml(Server.MapPath("~/App_Data/XMLFile.xml"))
61  BindGridView()
62 End If
63End Sub

Build and run the application.

Download Sample Code


If you like this post than join us or share

5 comments:

ekestler said... 1

I have a number of XML files with similar, but not exact, structure -- they all share common columns, but a few have more columns, each differing. For instance, all of them have Name, City, Title, but some of them also have Department, and one or two have Project. I'd like to use one solution for all of them...so how do I define the item templates dynamically?


The Commuter said... 2

Do you have a download link for vb?


Bijoy Viswanath said... 3

This will work if there is data already present in the database right?


Unknown said... 4

@Bijoy Viswanath: I have not used database but XML file for this example


Anonymous said... 5

Excellent work keep it up


Find More Articles