Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Binding a DataGrid to an XML file

This tutorial will show you how to bind a DataGrid to a simple XML file so you can display the values as rows and the tags as columns.

On Tuesday, November 2nd 2004 at 09:32 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.4 with 30 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
Start a new ASP .NET Web Application project. From the Solution Explorer window right click the project and choose Add -> New Item. From the Add New Item window choose XML File. In the name textbox enter Movies.xml. This will be the XML file we will work with and we're going to store information about some movies in it.







Now that it is added to the project, open it. This is the only line you'll find in it:





<?xml version="1.0" encoding="utf-8" ?>



I suppose you have at least the basic knowledge of the structure of an XML file if you're interested in reading it using ASP .NET and C#.

So inside the XML file let's add these 3 movies:





<?xml version="1.0" encoding="utf-8" ?>

<Movies>

   <Movie>

      <Title>

   
   The Godfather

      </Title>

      <Genre>

   
   Crime

      </Genre>

      <Year>

   
   1972

      </Year>

   </Movie>

   <Movie>

      <Title>

   
   The Green Mile

      </Title>

      <Genre>


   
   Fantasy

      </Genre>

      <Year>


   
   1999

      </Year>

   </Movie>


   <Movie>

      <Title>


   
   Cast Away

      </Title>

      <Genre>


   
   Adventure

      </Genre>

      <Year>


   
   2000

      </Year>

   </Movie>


</Movies>



Now open the main WebForm, WebForm1.aspx and drag a DataGrid on it from the Toolbox. As a result of adding the DataGrid the <asp:DataGrid> tag is created inside WebForm1.aspx:





<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server">

</asp:DataGrid>



We want the data inside the XML file to be displayed in the DataGrid. So open WebForm1.aspx.cs and we'll bind the DataGrid to the XML file.

We can see that the DataGrid DataGrid1 was created here:





protected System.Web.UI.WebControls.DataGrid DataGrid1;



So let's bind it. We'll do this in Page_Load.





private void Page_Load(object sender, System.EventArgs e)

{

   // Create a new DataSet to read from the XML

   System.Data.DataSet DSet = new DataSet();

   // Tell the DataReader from which file to read

   DSet.ReadXml(MapPath("Movies.xml"));



   // Set the DataSource property of the DataGrid to the DataSet

   DataGrid1.DataSource = DSet;

   // Bind

   DataGrid1.DataBind();

}



For reading the XML file we need a DataSet which we created. The commented code is really easy to follow.






i

MapPath retrieves the physical to the file specified, so it helped us here, as we just entered the relative path (Movie.xml) and not the entire path to the file.



Now run the code and see the result.








The code creates a simple DataGrid holding the data from the XML file.



Of course, most of the time you'll want to select the columns which you want to appear in the DataGrid, for example you might not be interested to have a column with the year in which the movie was created, you want just the title and genre. This is easily accomplished, the same way you do it when working with databases. You set the AutoGenerateColumns attribute to False and use <asp:BoundColumn> tags to select which tags / columns you want to be displayed:





<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" AutoGenerateColumns="False">

   <Columns>

      <asp:BoundColumn DataField="Title" HeaderText="Title" />

      <asp:BoundColumn DataField="Genre" HeaderText="Genre" />

   </Columns>

</asp:DataGrid>



Further, if you want to make the DataGrid more esthetic, the tutorial 'Basics of using DataGrid' will show you how.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by PrasadVarier on Wednesday, December 1st 2004 at 12:43 AM

Very good and useful.Can somebody help me by giving an asp.net project

by PrasadVarier on Wednesday, December 1st 2004 at 12:45 AM

Very good and useful.Can somebody help me by giving an asp.net project

by anil on Friday, July 1st 2005 at 10:15 AM

very useful indeed!

by Shawn McCUllough on Monday, August 15th 2005 at 04:05 PM

GREAT example.. Exactly what I needed.. THANX!

by frozenblood on Thursday, September 15th 2005 at 07:24 AM

It really helps a lot !

Great !

by sudhakar on Tuesday, November 8th 2005 at 07:34 AM

Good

by Losthat on Monday, December 5th 2005 at 02:05 PM

Thank you... short and sweet.

by chandan on Tuesday, December 6th 2005 at 01:12 AM

Good work...!!!!!!!!!

by DHALIWAL on Monday, March 6th 2006 at 04:39 PM

Short Key For A Big Lock

by LeProgrammeur on Sunday, June 11th 2006 at 08:39 PM

Binding XML to DataGrid

Hi guys!
The following link shows a tutorial that walks you through the steps to bind an xml file to a datagrid. The same website has a chat room and I try to spend a lot of time there answering people\'s question. Stop by if you want.
:)
http://www.kynou.com/GetTutorial.aspx?TutorialID=65

by fgfgg on Tuesday, March 6th 2007 at 11:58 PM

hey what are doing

by ashir on Wednesday, June 20th 2007 at 02:53 AM

what is wrong in the following code?

I need to bind an xml file to a datagrid.... However when I run this code in browser I just get a blank page.....


<%@ Page Language=\"VB\" AutoEventWireup=\"false\" CodeFile=\"Default.aspx.vb\" Inherits=\"_Default\" %>

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<%@Import Namespace=\"System.Data\" %>
<%@ Import Namespace=\"System.Data.SqlClient\" %>
<%@ Import Namespace=\"System.IO\" %>

<script runat=\"Server\">
Sub Page_Load ( Server As Object, E As EventArgs)
Dim fs As Filestream
fs = New FileStream(Server.MapPath(\"movies.xml\"), FileMode.Open, FileAccess.Read)

Dim Reader As StreamReader
Reader = New StreamReader(fs)
Dim ds As New DataSet
ds.ReadXml(Reader)
fs.Close()
Dim source As DataView
source = New DataView(ds.Tables(0))
DataGrid1.DataSource = source
DataGrid1.DataBind()
End Sub
</script>
<html xmlns=\"http://www.w3.org/1999/xhtml\" >
<head id=\"Head1\" runat=\"server\">
<title>Untitled Page</title>
</head>
<body>
<form id=\"form1\" runat=\"server\">
<div>
<asp:DataGrid ID=\"DataGrid1\" runat=\"server\" >
</asp:DataGrid></div>
</form>
</body>
</html>

by Vasanthi on Thursday, April 17th 2008 at 03:47 AM

Ya.Very Nice Example.

by Hamza on Monday, November 9th 2009 at 01:46 AM

hi all,
i want to write xml file from a grid using XMLTextWriter with vb.net...

best Regards
Hamza Malakwi

by deepak on Friday, July 30th 2010 at 10:08 AM

Hey thanks a lot...
well explained...


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources