For this example i have created a XML file with three fields Firstname,LastName and Location and XML file looks like mentioned below.
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.
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="gridviewXMLInsertEditDelete" runat="server" AutoGenerateColumns="False" ShowFooter="True" onRowCancelingEdit="gridviewXMLInsertEditDelete_RowCancelingEdit" onRowDeleting="gridviewXMLInsertEditDelete_RowDeleting" onRowEditing="gridviewXMLInsertEditDelete_RowEditing" onRowUpdating="gridviewXMLInsertEditDelete_RowUpdating" onRowCommand="gridviewXMLInsertEditDelete_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") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName") %>'> </asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="InsertXMLData"/> <asp:TextBox ID="txtFirstNameInsert" runat="server"> </asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Name"> <ItemTemplate> <asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("LastName") %>'> </asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtLastNameInsert" runat="server"> </asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Location"> <ItemTemplate> <asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLocation" runat="server" Text='<%#Bind("Location") %>'> </asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtLocationInsert" runat="server"> </asp:TextBox> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView>
c# CODE
01
protected
void
Page_Load(
object
sender, EventArgs e)
02
{
03
if
(!Page.IsPostBack)
04
BindGridView();
05
}
06
protected
void
BindGridView()
07
{
08
DataSet dsGridViewXMLEditDeleteUpdate =
new
DataSet();
09
dsGridViewXMLEditDeleteUpdate.ReadXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
));
10
gridviewXMLInsertEditDelete.DataSource = dsGridViewXMLEditDeleteUpdate;
11
gridviewXMLInsertEditDelete.DataBind();
12
gridviewXMLInsertEditDelete.ShowFooter =
true
;
13
}
14
15
protected
void
gridviewXMLInsertEditDelete_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
16
{
17
BindGridView();
18
DataSet dsGridViewXMLDelete = (DataSet)gridviewXMLInsertEditDelete.DataSource;
19
dsGridViewXMLDelete.Tables[0].Rows[gridviewXMLInsertEditDelete.Rows[e.RowIndex].DataItemIndex].Delete();
20
dsGridViewXMLDelete.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
));
21
BindGridView();
22
23
}
24
protected
void
gridviewXMLInsertEditDelete_RowEditing(
object
sender, GridViewEditEventArgs e)
25
{
26
gridviewXMLInsertEditDelete.ShowFooter =
false
;
27
gridviewXMLInsertEditDelete.EditIndex = e.NewEditIndex;
28
BindGridView();
29
}
30
protected
void
gridviewXMLInsertEditDelete_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
31
{
32
int
index = gridviewXMLInsertEditDelete.Rows[e.RowIndex].DataItemIndex;
33
string
firstName = ((TextBox)gridviewXMLInsertEditDelete.Rows[e.RowIndex].FindControl(
"txtFirstName"
)).Text;
34
string
lastName = ((TextBox)gridviewXMLInsertEditDelete.Rows[e.RowIndex].FindControl(
"txtLastName"
)).Text;
35
string
location = ((TextBox)gridviewXMLInsertEditDelete.Rows[e.RowIndex].FindControl(
"txtLocation"
)).Text;
36
gridviewXMLInsertEditDelete.EditIndex = -1;
37
BindGridView();
38
DataSet dsUpdateXMLFile = (DataSet)gridviewXMLInsertEditDelete.DataSource;
39
dsUpdateXMLFile.Tables[0].Rows[index][
"FirstName"
] = firstName;
40
dsUpdateXMLFile.Tables[0].Rows[index][
"LastName"
] = lastName;
41
dsUpdateXMLFile.Tables[0].Rows[index][
"Location"
] = location;
42
dsUpdateXMLFile.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
));
43
BindGridView();
44
45
}
46
protected
void
gridviewXMLInsertEditDelete_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
47
{
48
gridviewXMLInsertEditDelete.EditIndex = -1;
49
BindGridView();
50
}
51
52
protected
void
gridviewXMLInsertEditDelete_RowCommand(
object
sender, GridViewCommandEventArgs e)
53
{
54
55
if
(e.CommandName ==
"InsertXMLData"
)
56
{
57
string
firstname = ((TextBox)gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtFirstNameInsert"
)).Text;
58
string
lastname = ((TextBox)gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtLastNameInsert"
)).Text;
59
string
location = ((TextBox)gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtLocationInsert"
)).Text;
60
BindGridView();
61
DataSet dsXMLInsert = (DataSet)gridviewXMLInsertEditDelete.DataSource;
62
DataRow drInsert = dsXMLInsert.Tables[0].NewRow();
63
drInsert[
"FirstName"
] = firstname;
64
drInsert[
"LastName"
] = lastname;
65
drInsert[
"Location"
] = location;
66
dsXMLInsert.Tables[0].Rows.Add(drInsert);
67
dsXMLInsert.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
));
68
BindGridView();
69
}
70
}
VB.NET CODE
01
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
02
If
Not
Page.IsPostBack
Then
03
BindGridView()
04
End
If
05
End
Sub
06
Protected
Sub
BindGridView()
07
Dim
dsGridViewXMLEditDeleteUpdate
As
New
DataSet()
08
dsGridViewXMLEditDeleteUpdate.ReadXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
))
09
gridviewXMLInsertEditDelete.DataSource = dsGridViewXMLEditDeleteUpdate
10
gridviewXMLInsertEditDelete.DataBind()
11
gridviewXMLInsertEditDelete.ShowFooter =
True
12
End
Sub
13
14
Protected
Sub
gridviewXMLInsertEditDelete_RowDeleting(sender
As
Object
, e
As
GridViewDeleteEventArgs)
15
BindGridView()
16
Dim
dsGridViewXMLDelete
As
DataSet =
DirectCast
(gridviewXMLInsertEditDelete.DataSource, DataSet)
17
dsGridViewXMLDelete.Tables(0).Rows(gridviewXMLInsertEditDelete.Rows(e.RowIndex).DataItemIndex).Delete()
18
dsGridViewXMLDelete.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
))
19
BindGridView()
20
21
End
Sub
22
Protected
Sub
gridviewXMLInsertEditDelete_RowEditing(sender
As
Object
, e
As
GridViewEditEventArgs)
23
gridviewXMLInsertEditDelete.ShowFooter =
False
24
gridviewXMLInsertEditDelete.EditIndex = e.NewEditIndex
25
BindGridView()
26
End
Sub
27
Protected
Sub
gridviewXMLInsertEditDelete_RowUpdating(sender
As
Object
, e
As
GridViewUpdateEventArgs)
28
Dim
index
As
Integer
= gridviewXMLInsertEditDelete.Rows(e.RowIndex).DataItemIndex
29
Dim
firstName
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.Rows(e.RowIndex).FindControl(
"txtFirstName"
), TextBox).Text
30
Dim
lastName
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.Rows(e.RowIndex).FindControl(
"txtLastName"
), TextBox).Text
31
Dim
location
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.Rows(e.RowIndex).FindControl(
"txtLocation"
), TextBox).Text
32
gridviewXMLInsertEditDelete.EditIndex = -1
33
BindGridView()
34
Dim
dsUpdateXMLFile
As
DataSet =
DirectCast
(gridviewXMLInsertEditDelete.DataSource, DataSet)
35
dsUpdateXMLFile.Tables(0).Rows(index)(
"FirstName"
) = firstName
36
dsUpdateXMLFile.Tables(0).Rows(index)(
"LastName"
) = lastName
37
dsUpdateXMLFile.Tables(0).Rows(index)(
"Location"
) = location
38
dsUpdateXMLFile.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
))
39
BindGridView()
40
41
End
Sub
42
Protected
Sub
gridviewXMLInsertEditDelete_RowCancelingEdit(sender
As
Object
, e
As
GridViewCancelEditEventArgs)
43
gridviewXMLInsertEditDelete.EditIndex = -1
44
BindGridView()
45
End
Sub
46
47
Protected
Sub
gridviewXMLInsertEditDelete_RowCommand(sender
As
Object
, e
As
GridViewCommandEventArgs)
48
49
If
e.CommandName =
"InsertXMLData"
Then
50
Dim
firstname
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtFirstNameInsert"
), TextBox).Text
51
Dim
lastname
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtLastNameInsert"
), TextBox).Text
52
Dim
location
As
String
=
DirectCast
(gridviewXMLInsertEditDelete.FooterRow.FindControl(
"txtLocationInsert"
), TextBox).Text
53
BindGridView()
54
Dim
dsXMLInsert
As
DataSet =
DirectCast
(gridviewXMLInsertEditDelete.DataSource, DataSet)
55
Dim
drInsert
As
DataRow = dsXMLInsert.Tables(0).NewRow()
56
drInsert(
"FirstName"
) = firstname
57
drInsert(
"LastName"
) = lastname
58
drInsert(
"Location"
) = location
59
dsXMLInsert.Tables(0).Rows.Add(drInsert)
60
dsXMLInsert.WriteXml(Server.MapPath(
"~/App_Data/XMLFile.xml"
))
61
BindGridView()
62
End
If
63
End
Sub
0 comments:
Post a Comment