.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Bindind Xml File with gridview with update delete

Posted By:Rajshree       Posted Date: September 14, 2010    Points: 10    Category: DataGrid    URL: http://www.dotnetspark.com  

Bind XML File with GridView for update delete operation in asp.net with c#
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class ExWithGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getdata();
        }
    }
    private void getdata()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        string id = row.Cells[1].Text.Trim();
        
        XmlDocument doc2 = new XmlDocument();
        doc2.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        XmlNode nodeList = doc2.SelectSingleNode("/catalog/book[@id='" + id + "']");
        doc2.DocumentElement.RemoveChild(nodeList);
        doc2.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        getdata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        getdata();
    }
   
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
              getdata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        string pub = row.Cells[6].Text.Trim();
        XmlDocument doc1 = new XmlDocument();
        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");
        nodeList[0].ChildNodes[4].InnerText = pub;

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        getdata();
    }
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        string pub = (row.Cells[6].Controls[0] as TextBox).Text;
        string a = row.Cells[2].Text;
        XmlDocument doc1 = new XmlDocument();
        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");
        nodeList[0].ChildNodes[4].InnerText = pub;
        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");
        getdata();
    }
    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        getdata();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getdata();

    }
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend