Posted By

tabook22 on 07/11/11


Tagged

xml VBNet aspnet


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mgoblue


Binding xml file to Gridview


 / Published in: VB.NET
 

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

  1. 'Method Creating Xml File if does'nt Exists
  2. Private Sub createxml()
  3. Dim tb As New DataTable("emptable")
  4. tb.Columns.Add("eno", Type.[GetType]("System.Int32"))
  5. tb.Columns.Add("ename", Type.[GetType]("System.String"))
  6. tb.Columns.Add("eadd", Type.[GetType]("System.String"))
  7. tb.Columns.Add("esal", Type.[GetType]("System.Int32"))
  8. Dim r As DataRow = tb.NewRow()
  9. r(0) = 1
  10. r(1) = "Rahul Choudhary"
  11. r(2) = "Chandigarh"
  12. r(3) = 12000
  13. tb.Rows.Add(r)
  14. Dim st As [String] = Server.MapPath("emp.xml")
  15. tb.WriteXml(st)
  16. End Sub
  17.  
  18. 'Method to bind Grid view
  19. Private Sub getxml()
  20. Dim st As [String] = Server.MapPath("emp.xml")
  21. Dim ds As New DataSet()
  22. ds.ReadXml(st)
  23. GridView1.DataSource = ds
  24. GridView1.DataBind()
  25. End Sub
  26.  
  27. 'Grid View Event to Save Data into Xml File
  28. Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
  29. If e.CommandName = "save" Then
  30. Dim st As [String] = Server.MapPath("emp.xml")
  31. Dim ds As New DataSet()
  32. ds.ReadXml(st)
  33. Dim r As DataRow = ds.Tables(0).NewRow()
  34. r(0) = Convert.ToInt32(DirectCast(GridView1.FooterRow.FindControl("TextBox1"), TextBox).Text)
  35. r(1) = DirectCast(GridView1.FooterRow.FindControl("TextBox3"), TextBox).Text
  36. r(2) = DirectCast(GridView1.FooterRow.FindControl("TextBox5"), TextBox).Text
  37. r(3) = Convert.ToInt32(DirectCast(GridView1.FooterRow.FindControl("TextBox7"), TextBox).Text)
  38. ds.Tables(0).Rows.Add(r)
  39. ds.WriteXml(st)
  40. getxml()
  41. End If
  42. End Sub
  43.  
  44. 'Grid View Row Deleting Event to Delete From Xml File
  45. Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
  46. Dim st As [String] = Server.MapPath("emp.xml")
  47. Dim ds As New DataSet()
  48. ds.ReadXml(st)
  49. ds.Tables(0).Rows.RemoveAt(e.RowIndex)
  50. ds.WriteXml(st)
  51. getxml()
  52. End Sub
  53. Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
  54. GridView1.EditIndex = e.NewEditIndex
  55. getxml()
  56. End Sub
  57. Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
  58. GridView1.EditIndex = -1
  59. getxml()
  60. End Sub
  61.  
  62. 'Grid View row Updating Event to update xml file
  63. Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
  64. Dim st As [String] = Server.MapPath("emp.xml")
  65. Dim ds As New DataSet()
  66. ds.ReadXml(st)
  67. ds.Tables(0).Rows(e.RowIndex)(1) = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
  68. ds.Tables(0).Rows(e.RowIndex)(2) = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox4"), TextBox).Text
  69. ds.Tables(0).Rows(e.RowIndex)(3) = Convert.ToInt32(DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox6"), TextBox).Text)
  70. GridView1.EditIndex = -1
  71. ds.WriteXml(st)
  72. getxml()
  73. End Sub

Report this snippet  

You need to login to post a comment.