Click here to Skip to main content
13,704,420 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 15 Jan 2009
Licenced CPOL

Using XML as datagridview Source

, 15 Jan 2009
Rate this:
Please Sign up or sign in to vote.
An article about linking datagridview with XML file


As you know, XML is a multi-purpose language that is recommended by the World Wide Web Consortium (W3C). It is free, and we can read it in many applications. In this article, I'd like to tell you about the connection between DataGridView and XML file. Usually we use the DataSource from table which we can get from SQL Server database, Oracle, MySQL, or any other database. This time, I'd like to introduce something different. I believe some of you have known this method, but I think it's okay to post this article for beginners. Instead of using a common database language, let's try to use XML.

Using the Code

So let's start making a simple application that uses XML as the database. The example here, which you can download from the attachment, uses one table, that I name as User. That table consists of two columns, one is named user_name, and the other one is user_password.  

First, you can look below at the format of user.xml that I use as the DataSource:

<?xml version="1.0" standalone="yes"?>

In XML, we'd have to make the structure like a tree. It starts with the root (Table), and then the parent (User), and the child (user_name, user_password). From the structure above, we know that we have one table that consists of two columns with two rows of data.

When we execute our code, through the load event, the program will check if the XML file exists, or not. If it does not exist, we should make an empty table. I create a function as shown below. I create 2 parameters, TableName and ColumnNames that I declare as a ParamArray. ParamArray, if you haven't known this kind of type, is a parameter that can catch multi-values. Okay, back to the topic. When the program knows that we don't have the file, it will call this function. This will create the tree-structured XML, with an empty string value, that is used only as an initialization.

Private Function CreateEmptyTable(ByVal TableName As String, _
	ByVal ParamArray ColumnNames() As String) As Boolean
  Dim writer As New XmlTextWriter(filePath, System.Text.Encoding.UTF8)

    writer.Formatting = Formatting.Indented
    writer.Indentation = 2
    For Each ColumnName As String In ColumnNames
  Catch ex As Exception
    MsgBox("Cannot create new table.")
    Return False
  End Try

  Return True
End Function

Then after running the validation above, we'll read the content of the XML, and make it as the DataSource of the DataGridView that we have in our form.

xmlFile = XmlReader.Create(filePath, New XmlReaderSettings())

Me.DgvMstUser.DataSource = ds.Tables(0)

For the explanation of code above, we create xmlFile based on the content of the file that we have before. Then read it and save it into the dataset. Just don't forget to close the xmlFile, because if you do, there will be an error saying that the file is currently used by another application. After we get the dataset, we call the first table inside. Since we only have one table, and we'd like to call that first table, we call it using command .Tables(0) on the DataSet.  

Run it, and you have a DataGridView with an XML datasource. Now what next? After we can select the data, how do you update the data? You can use a simple command that I provide below.  

Dim dt As New DataTable

dt = Me.DgvMstUser.DataSource

We create a new DataTable object, get the DataSource table from the DataGridView, and write it as an XML.  

And that's all. Congratulations, you have made a simple application using an XML database. If you have any questions about this article, feel free to email me or post your comments.


  • 15th January, 2009: Initial post 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Indra PR
Software Developer (Junior) PT. Televisi Transformasi Indonesia
Indonesia Indonesia
Indra Permana Rusli is a freshgraduate, has experience and knowledge in using multimedia softwares, developing website using PHP, and currently working using VB.NET in his company to develop ERP software.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
ProJester115-Jan-09 11:05
memberProJester115-Jan-09 11:05 
GeneralRe: My vote of 2 Pin
saini arun16-Jan-09 19:02
membersaini arun16-Jan-09 19:02 
GeneralRe: My vote of 2 Pin
Jon_Boy9-Feb-09 7:16
memberJon_Boy9-Feb-09 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 15 Jan 2009
Article Copyright 2009 by Indra PR
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid