Wednesday, April 4, 2012

DML Operations in gridView with xml file


xml file】




Corets, Eva
The Sundered Grail
Fantasy
5.95
2001-09-10

The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.



Thurman, Paula
Splish Splash
Romance
4.95
2000-11-02

A deep sea diver finds true love twenty
thousand leagues beneath the sea.



Knorr, Stefan
Creepy Crawlies
Horror
4.95
2000-12-06

An anthology of horror stories about roaches,
centipedes, scorpions and other insects.



Kress, Peter
Paradox Lost
Science Fiction
6.95
2000-11-02

After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.



O'Brien, Tim
Microsoft .NET: The Programming Bible
Computer
36.95
2000-12-09

Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.



O'Brien, Tim
MSXML3: A Comprehensive Guide
Computer
36.95
2000-12-01

The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.



Galos, Mike
Visual Studio 7: A Comprehensive Guide
Computer
49.95
2001-04-16

Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.






[.cs file]

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XmlEditInsertDeleteInGridView
{
public partial class Default : System.Web.UI.Page
{
///
/// For the first time when the page loads, load data into DataTable
/// with DataSet, and save the DataTable into ViewState for further
/// usage.
///

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Request.MapPath("try.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["dt"] = ds.Tables[0];
}
}

///
/// Handle the Edit event of GridView for assigning the specific row to be
/// in the edit mode.
///

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}

///
/// Update the specific row in the DataTable with the data from GridView,
/// re-write the data into the xml file and re-databind again.
///

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];

for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
{
dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
}
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
dt.WriteXml(Request.MapPath("try.xml"));
}

///
/// Cancel edit and set the mode of the GridView to normal viewing mode.
///

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}

///
/// Insert the data into the DataTable, re-write into the xml file and
/// re-databind to the GridView.
///

protected void btnInsert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
dt.AcceptChanges();
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}

///
/// Delete the row from DataTable and write data into xml file,
/// re-databind to the GridView.
///

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.RemoveAt(e.RowIndex);
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

【aspx file】

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlEditInsertDeleteInGridView.Default" %>




Xml-based CRUD Code Sample







Xml-based CRUD Code Sample——


AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">












Id:



author:



title:



genre:



price:



publishdate:



description:


Text="Insert" Width="141px" />