Thursday, August 20, 2009

How to bind XML Data to GridView or DataGrid?

Hi Readers,

Thank you for reading my blog.I received 40 emails yesterday.

Here is how to bind xml data to GridView or DataGrid:

Download aspnetbooks.xml source code here

Step 1:

-Add reference to System.Data and System.XML
imports System.Data
imports System.Xml

In C# :
using System.Data;
using System.Xml;

-Drop a GridView or DataGrid on designer surface
-Double click on designer for Page_Load event

Step3:Add this code inside Page_Load


Dim ds As New DataSet
Dim XMLMode As New System.Data.XmlReadMode
Me.GridView1.DataSource = ds

I programmed GetXMLPath function which is:

Protected Function GetXMLPath(ByVal xmlpath As String) As String
Return Request.PhysicalApplicationPath & xmlpath
End Function

In C#:

DataSet ds=new DataSet();
System.Data.XmlReadMode XMLMode=new System.Data.XmlReadMode();
this.GridView1.DataSource = ds

for the function:

protected string GetXMLPath(string xmlpath)
return Request.PhysicalApplicationPath + xmlpath

Note + sign instead of & inside GetXMLPath method(C# uses + while VB.NET uses &)

The code inside VS should look like this:

The output should look like this:

Thank you for reading.

Any question or if you need private online training please email me at

Talley Ouro

No comments:

About Me

My photo
Raleigh, NC, United States
I am a software developer based in Raleigh,NC,USA.I design softwares and systems ;i also do consulting for companies worldwide.I program with these languages:VB.NET 2003/2005/2008;C#;Java(fun),SQL(200,2005,2008);ASP.NET 2.0/3.5;ASP.NET AJAX;ASP.NET MVC;JavaScript;JQuery;Windows Workflow Foundation;Web Services.I have 4 years + in programming.