Try out below code. It should work.
Only 2 issues in code converted from above link provided by
OriginalGriff
1. GridView row/cell accessing in code behind should be with square brackets(i.e. "[". "]")
2. GridView source should be parse to DataSet. [ I am not sure if it will work or not :( ]
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.UI.WebControls;
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
DataSet oDs = new DataSet();
oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml");
GridView1.DataSource = oDs;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
BindGrid();
DataSet oDs = (DataSet)GridView1.DataSource;
oDs.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
BindGrid();
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
int i = GridView1.Rows[e.RowIndex].DataItemIndex;
string strId = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string strTel = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
GridView1.EditIndex = -1;
BindGrid();
DataSet oDs = (DataSet)GridView1.DataSource;
oDs.Tables[0].Rows[i][0] = strId;
oDs.Tables[0].Rows[i][1] = strName;
oDs.Tables[0].Rows[i][2] = strTel;
oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
BindGrid();
}
protected void Button1_Click(object sender, System.EventArgs e)
{
BindGrid();
DataSet oDs = (DataSet)GridView1.DataSource;
DataRow oDr = oDs.Tables[0].NewRow();
oDr["id"] = txtId.Text;
oDr["name"] = txtName.Text;
oDr["phone"] = txtTel.Text;
oDs.Tables[0].Rows.Add(oDr);
oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
BindGrid();
}
}
Hope it helps.