Logo

XML and the Data Grid

 

Introduction

A data grid is the prime candidate to display values of an XML file. It displays its values as a table, using columns and rows.

 

Using a Data Grid

To display the values of an XML file in a data grid, once the values have been loaded through a data set, you can use the DataSource property, in which case you would simply assign the name of the DataSet variable to the DataGrid control.

  1. Start Microsoft Visual C++ .Net or MS Visual Studio .Net and create a new Windows Forms Application named IntroXML1 with its default form
  2. To create an XML file, on the main menu, click Project -> Add New Item...
  3. In the Templates list, click XML File (.xml)
  4. In the Name box, replace the string with employees
     
    Add New Item
  5. Click Open
  6. Click the empty line under the first. Type <fullrecord>
  7. Notice that as soon as you type the > sign, the closing tag is created. Complete the file as follows:
     
    <?xml version="1.0" encoding="utf-8"?>
    <fullrecord>
      <employee>
        <firstname>Sylvie</firstname>
        <lastname>Aronson</lastname>
        <salary>25.64</salary>
        <gender>Female</gender>
      </employee>
      <employee>
        <firstname>Bertrand</firstname>
        <lastname>Yamaguchi</lastname>
        <salary>16.38</salary>
        <gender>Male</gender>
      </employee>
        <firstname>Anselme</firstname>
        <lastname>Bean</lastname>
        <salary>22.82</salary>
        <gender>Male</gender>
      <employee>
        <firstname>Mauricette</firstname>
        <lastname>Thomas</lastname>
        <salary>18.35</salary>
        <gender>Female</gender>
      </employee>
      <employee>
        <firstname>Hermine</firstname>
        <lastname>Gray</lastname>
        <salary>12.75</salary>
        <gender>Female</gender>
      </employee>
    </fullrecord>
  8. Display the form
  9. From the Windows Forms section of the Toolbox, click DataGrid and click the form:
     
  10. Right-click the form and click View Code
  11. To use a data set, declare a DataSet pointer in the form outside the constructor:
     
    #pragma once
    
    
    namespace IntroXML1
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::DataGrid *  dataGrid1;
    		DataSet *dsEmployees;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			. . .
    
    		}	
    	};
    }
  12. Display the form
  13. Double-click any empty area on the form and implement its Load event as follows:
     
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->dsEmployees = new DataSet(S"employees");
    	 this->dsEmployees->ReadXml(S"employees.xml");
    	 this->dataGrid1->DataSource = this->dsEmployees;
    	 this->dataGrid1->DataMember = S"employee";
    }
  14. Test the application
     
  15. Close the form
 

Home Copyright © 2004-2010 FunctionX, Inc.