Return to Snippet

Revision: 48826
at July 11, 2011 06:44 by tabook22


Initial Code
'Method Creating Xml File if does'nt Exists
Private Sub createxml()
	Dim tb As New DataTable("emptable")
	tb.Columns.Add("eno", Type.[GetType]("System.Int32"))
	tb.Columns.Add("ename", Type.[GetType]("System.String"))
	tb.Columns.Add("eadd", Type.[GetType]("System.String"))
	tb.Columns.Add("esal", Type.[GetType]("System.Int32"))
	Dim r As DataRow = tb.NewRow()
	r(0) = 1
	r(1) = "Rahul Choudhary"
	r(2) = "Chandigarh"
	r(3) = 12000
	tb.Rows.Add(r)
	Dim st As [String] = Server.MapPath("emp.xml")
	tb.WriteXml(st)
End Sub

'Method to bind Grid view
Private Sub getxml()
	Dim st As [String] = Server.MapPath("emp.xml")
	Dim ds As New DataSet()
	ds.ReadXml(st)
	GridView1.DataSource = ds
	GridView1.DataBind()
End Sub

'Grid View Event to Save Data into Xml File
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
	If e.CommandName = "save" Then
		Dim st As [String] = Server.MapPath("emp.xml")
		Dim ds As New DataSet()
		ds.ReadXml(st)
		Dim r As DataRow = ds.Tables(0).NewRow()
		r(0) = Convert.ToInt32(DirectCast(GridView1.FooterRow.FindControl("TextBox1"), TextBox).Text)
		r(1) = DirectCast(GridView1.FooterRow.FindControl("TextBox3"), TextBox).Text
		r(2) = DirectCast(GridView1.FooterRow.FindControl("TextBox5"), TextBox).Text
		r(3) = Convert.ToInt32(DirectCast(GridView1.FooterRow.FindControl("TextBox7"), TextBox).Text)
		ds.Tables(0).Rows.Add(r)
		ds.WriteXml(st)
		getxml()
	End If
End Sub

'Grid View Row Deleting Event to Delete From Xml File
Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
	Dim st As [String] = Server.MapPath("emp.xml")
	Dim ds As New DataSet()
	ds.ReadXml(st)
	ds.Tables(0).Rows.RemoveAt(e.RowIndex)
	ds.WriteXml(st)
	getxml()
End Sub
Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
	GridView1.EditIndex = e.NewEditIndex
	getxml()
End Sub
Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
	GridView1.EditIndex = -1
	getxml()
End Sub

'Grid View row Updating Event to update xml file
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
	Dim st As [String] = Server.MapPath("emp.xml")
	Dim ds As New DataSet()
	ds.ReadXml(st)
	ds.Tables(0).Rows(e.RowIndex)(1) = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
	ds.Tables(0).Rows(e.RowIndex)(2) = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox4"), TextBox).Text
	ds.Tables(0).Rows(e.RowIndex)(3) = Convert.ToInt32(DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox6"), TextBox).Text)
	GridView1.EditIndex = -1
	ds.WriteXml(st)
	getxml()
End Sub

Initial URL

                                

Initial Description
This code will be used to bind an xml file to gridview

Initial Title
Binding xml file to Gridview

Initial Tags
xml, aspnet

Initial Language
VB.NET