Click here to Skip to main content
Licence CPOL
First Posted 13 Aug 2008
Views 9,770
Downloads 147
Bookmarked 8 times

Using DataGrids to display records in XML databases in VC++ 2008

By | 1 Sep 2008 | Article
This application will display XML data in a table format and write tabular data to XML documents.

DataGrids_in_XML

Introduction

In this article, I will show how to use Visual C++ 2008 to represent your XML data in tabular format. Many examples use C#, and amateur developers of C++/CLI applications may not know how to translate the code.

Background

XML databases are crucial, especially for Web applications and portability. Further still, they are quite useful for low RAM computing systems. The .NET platform contains predefined functions that will enable us to read directly and write to an XML database from a DataGrid contained in a Windows form.

Code

Use the XML file "FILMS.xml" provided in the zip archive.

Remember to use the following namespaces; otherwise, XML methods will not be recognized.

using namespace System::Data::SqlClient;
using namespace System::Xml;

I have chosen to hide the load button after the display of the records is complete, because pressing it twice will add onto the already existing data. I am still trying to get a way round this... I will let you know!

Please remember to replace:

"D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml"

with your own path that gets to the XML file.

XmlDataDocument mydoc; 

private: System::Void LoadDataButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
    try 
    {
        mydoc.DataSet->ReadXml("D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml");

        XMLDataGridView->DataSource = mydoc.DataSet->DefaultViewManager;
        XMLDataGridView->DataMember = "FILM";
        LoadDataButton->Hide();
    }
    catch(Exception^ ex)
    {
        MessageBox::Show("Error:"+ ex->Message);
    }
}

private: System::Void SaveDataButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
    mydoc.DataSet->WriteXml("D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml");
    //this function will get data from the Grid and Update in well formed XML format
}

Points of Interest

WriteXML auto-translates the edited table to well formed XML! So, you don't have to keep on making new tags and all in your XML document... Just put all your data in the table and click "Save"!!!

Note

I am still trying to get it to accept records from Excel such that we can copy and paste at a go and save in XML in just a second.

License

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

About the Author

Maximus Byamukama

Engineer

United Kingdom United Kingdom

Member

I have deserted general software development and chosen to enter microprocessors and push around 1s and 0s

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 2 Sep 2008
Article Copyright 2008 by Maximus Byamukama
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid