Datagrid Xml : Xml DataGrid « XML « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;

  public class XmlDataGridForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.DataGrid dgLateXmlBound;
    private System.Windows.Forms.Button cmdLoad;
    private System.Windows.Forms.Button cmdSave;
    private XmlDataDocument xdocSnowReports;
    private System.Windows.Forms.DataGridTableStyle dgsResorts;
    private System.Windows.Forms.DataGridTextBoxColumn tbcResort;
    private System.Windows.Forms.DataGridTextBoxColumn tbcSnowMin;
    private System.Windows.Forms.DataGridTextBoxColumn tbcSnowHigh;

    public XmlDataGridForm()
    {
      this.dgLateXmlBound = new System.Windows.Forms.DataGrid();
      this.cmdLoad = new System.Windows.Forms.Button();
      this.cmdSave = new System.Windows.Forms.Button();
      this.dgsResorts = new System.Windows.Forms.DataGridTableStyle();
      this.tbcResort = new System.Windows.Forms.DataGridTextBoxColumn();
      this.tbcSnowMin = new System.Windows.Forms.DataGridTextBoxColumn();
      this.tbcSnowHigh = new System.Windows.Forms.DataGridTextBoxColumn();
      ((System.ComponentModel.ISupportInitialize)(this.dgLateXmlBound)).BeginInit();
      this.SuspendLayout();
      // 
      // dgLateXmlBound
      // 
      this.dgLateXmlBound.DataMember = "";
      this.dgLateXmlBound.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dgLateXmlBound.Name = "dgLateXmlBound";
      this.dgLateXmlBound.Size = new System.Drawing.Size(432, 264);
      this.dgLateXmlBound.TabIndex = 0;
      this.dgLateXmlBound.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                                                     this.dgsResorts});
      // 
      // cmdLoad
      // 
      this.cmdLoad.Location = new System.Drawing.Point(8, 280);
      this.cmdLoad.Name = "cmdLoad";
      this.cmdLoad.TabIndex = 1;
      this.cmdLoad.Text = "load";
      this.cmdLoad.Click += new System.EventHandler(this.cmdLoad_Click);
      // 
      // cmdSave
      // 
      this.cmdSave.Location = new System.Drawing.Point(96, 280);
      this.cmdSave.Name = "cmdSave";
      this.cmdSave.TabIndex = 2;
      this.cmdSave.Text = "save";
      this.cmdSave.Click += new System.EventHandler(this.cmdSave_Click);
      // 
      // dgsResorts
      // 
      this.dgsResorts.DataGrid = this.dgLateXmlBound;
      this.dgsResorts.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                                     this.tbcResort,
                                                     this.tbcSnowMin,
                                                     this.tbcSnowHigh});
      this.dgsResorts.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dgsResorts.MappingName = "Resort";
      // 
      // tbcResort
      // 
      this.tbcResort.Format = "";
      this.tbcResort.FormatInfo = null;
      this.tbcResort.HeaderText = "Resort name";
      this.tbcResort.MappingName = "Name";
      this.tbcResort.Width = 75;
      // 
      // tbcSnowMin
      // 
      this.tbcSnowMin.Format = "";
      this.tbcSnowMin.FormatInfo = null;
      this.tbcSnowMin.HeaderText = "Min snow";
      this.tbcSnowMin.MappingName = "SnowLow";
      this.tbcSnowMin.Width = 75;
      // 
      // tbcSnowHigh
      // 
      this.tbcSnowHigh.Format = "";
      this.tbcSnowHigh.FormatInfo = null;
      this.tbcSnowHigh.HeaderText = "Max snow";
      this.tbcSnowHigh.MappingName = "SnowHigh";
      this.tbcSnowHigh.Width = 75;
      // 
      // XmlDataGridForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(432, 309);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdSave,
                                      this.cmdLoad,
                                      this.dgLateXmlBound});
      this.Name = "XmlDataGridForm";
      this.Text = "XML in DataGrid";
      ((System.ComponentModel.ISupportInitialize)(this.dgLateXmlBound)).EndInit();
      this.ResumeLayout(false);

      xdocSnowReports = new XmlDataDocument();
    }
    static void Main() 
    {
      Application.Run(new XmlDataGridForm());
    }

    private void cmdLoad_Click(object sender, System.EventArgs e)
    {
      xdocSnowReports.DataSet.ReadXml(new StreamReader("SampleData.xml"), XmlReadMode.InferSchema);
      dgLateXmlBound.SetDataBinding(xdocSnowReports.DataSet, "Resort");

      DataRow dr = xdocSnowReports.DataSet.Tables["SnowReports"].Rows[0];
      string strCaption = dr["Date"].ToString();
      this.dgLateXmlBound.CaptionText = "Snow report on " + strCaption;
    }

    private void cmdSave_Click(object sender, System.EventArgs e)
    {
      xdocSnowReports.DataSet.WriteXml("hardcodedfilename.xml");
    }
  }








30.27.Xml DataGrid
30.27.1.Load Xml document to DataGrid
30.27.2.Datagrid Xml