In this article, I will show to how to insert, update, delete in gridview in asp.net with c# code.
First step you should create an xml file with column fields and save it as mytask.xml and then you will bind the xml file data to a Gridview and perform CURD Operation(insert,update,delete).
Copy and paste the following code:
Assigntask.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.multiLine {
height: 50px;
width: 250px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:GridView ID="XmlGridView" runat="server" AutoGenerateColumns="false"
Height="247px" Width="795px" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Vertical" ShowFooter="true"
OnRowCancelingEdit="XmlGridView_RowCancelingEdit" OnRowDeleting="XmlGridView_RowDeleting"
OnRowEditing="XmlGridView_RowEditing" OnRowUpdating="XmlGridView_RowUpdating">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="LblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="LblTitle" runat="server" Text='<%# Bind("Title")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtEditTitle" CssClass="multiLine" TextMode="MultiLine" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtTitle" TextMode="MultiLine" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="LblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtEditDescription" CssClass="multiLine" TextMode="MultiLine" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:Label ID="LblUrl" runat="server" Text='<%# Bind("Url") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtEditUrl" CssClass="multiLine" TextMode="MultiLine" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtUrl" TextMode="MultiLine" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Operations">
<ItemTemplate>
<asp:Button ID="BtnEdit" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="BtnDelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" runat="server" Text="Insert" OnClick="BtnInsert_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Assigntask.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack == true)
{
Get_Xml();
}
}
void Get_Xml()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Content/Mytask.xml"));
if (ds != null && ds.HasChanges())
{
XmlGridView.DataSource = ds;
XmlGridView.DataBind();
}
else
{
XmlGridView.DataBind();
}
}
protected void BtnInsert_Click(object sender, EventArgs e)
{
Insert_XML();
}
void Insert_XML()
{
TextBox Task_ID = XmlGridView.FooterRow.FindControl("TxtID") as TextBox;
TextBox Task_Title = XmlGridView.FooterRow.FindControl("TxtTitle") as TextBox;
TextBox Task_Description = XmlGridView.FooterRow.FindControl("TxtDescription") as TextBox;
TextBox Task_Url = XmlGridView.FooterRow.FindControl("TxtUrl") as TextBox;
XmlDocument MyXmlDocument = new XmlDocument();
MyXmlDocument.Load(Server.MapPath("~/Content/Mytask.xml"));
XmlElement ParentElement = MyXmlDocument.CreateElement("Task");
Get_Xml();
DataSet ds = XmlGridView.DataSource as DataSet;
DataRow dr = (DataRow)ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1];
XmlElement ID = MyXmlDocument.CreateElement("ID");
int x = Int32.Parse(dr["ID"].ToString()); // parse string to integer c#
x++; // incrementing value
ID.InnerText = x.ToString();//assigning incremented valuee to the xml field
XmlElement Title = MyXmlDocument.CreateElement("Title");
Title.InnerText = Task_Title.Text;
XmlElement Description = MyXmlDocument.CreateElement("Description");
Description.InnerText = Task_Description.Text;
XmlElement Url = MyXmlDocument.CreateElement("Url");
Url.InnerText = Task_Url.Text;
ParentElement.AppendChild(ID);
ParentElement.AppendChild(Title);
ParentElement.AppendChild(Description);
ParentElement.AppendChild(Url);
MyXmlDocument.DocumentElement.AppendChild(ParentElement);
MyXmlDocument.Save(Server.MapPath("~/Content/Mytask.xml"));
Get_Xml();
}
protected void XmlGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
XmlGridView.EditIndex = e.NewEditIndex;
Get_Xml();
}
protected void XmlGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
XmlGridView.EditIndex = -1;
Get_Xml();
}
protected void XmlGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = XmlGridView.Rows[e.RowIndex].DataItemIndex;
TextBox Title = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditTitle") as TextBox;
TextBox Description = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditDescription") as TextBox;
TextBox Url = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditUrl") as TextBox;
XmlGridView.EditIndex = -1;
Get_Xml();
DataSet ds = XmlGridView.DataSource as DataSet;
ds.Tables[0].Rows[id]["Title"] = Title.Text;
ds.Tables[0].Rows[id]["Description"] = Description.Text;
ds.Tables[0].Rows[id]["Url"] = Url.Text;
ds.WriteXml(Server.MapPath("~/Content/Mytask.xml"));
Get_Xml();
}
protected void XmlGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Get_Xml();
DataSet ds = XmlGridView.DataSource as DataSet;
ds.Tables[0].Rows[XmlGridView.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("~/Content/Mytask.xml"));
Get_Xml();
}
Output:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article