none
Creating an empty but databound DataGridView RRS feed

  • Question

  • I have a datagridview which I have successfully bound to an XML datasource.  I am able to insert, update and delete rows no problem.  I then save the data back to my XML document.

     

    However how do I go about binding the datagridview when my XML datasource is empty.  I.e. I wish to start with an empty datagridview add some new rows and then save to my XML document.

     

    Here's a sample of my code so far which loads and saves the XML fine.

     

    Any help would be appreciated

     

    Charlie

     

    Loading the XML and Binding the DataGridView

    DataSet ds = new DataSet();

    ds.ReadXml(fileName);

     

    dataGridView1.DataSource = ds;

    dataGridView1.DataMember = "product";

     

    Saving the data

    DataSet ds = (DataSet)dgvFunction.DataSource;

    ds.WriteXml(fileName);

    Thursday, August 9, 2007 8:04 AM

Answers

  • Hi, openshac,

     

    I think you should read a XMLSchema to tell the Data Structure of your xml.

    And as there is only one DataTable in your DataSet, a DataTable object is enough.

    For example.

     

    Code Snippet

                DataSet data = new DataSet();

                data.ReadXmlSchema("Schema.xsd");

                dataGridView1.DataSource = data.Tables[0];

     

     

    and it can be output like

    Code Snippet

            void button_Click(object sender, EventArgs e)

            {

                DataTable dt = (DataTable)dataGridView1.DataSource;

                dt.WriteXml("result.xml");

            }

     

     

    The sample for XMLSchema

    Code Snippet

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

    <xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">

         <xs:element name="xmlTable">

             <xs:complexType>

                  <xs:sequence>

                       <xs:element name="ID" type="xs:int" />

                       <xs:element name="Name" type="xs:string" />

                       <xs:element name="Price" type="xs:string" />

                       <xs:element name="CreateTime" type="xs:dateTime" />

                  </xs:sequence>

             </xs:complexType>

             <xs:key name="xmlTableKey1">

                  <xs:selector xpath="." />

                  <xs:field xpath="mstns:ID" />

             </xs:key>

         </xs:element>

    </xs:schema>

     

     

    Hope this helps,

    Regards

    Wednesday, August 15, 2007 8:51 AM

All replies

  • Hi, openshac,

     

    I think you should read a XMLSchema to tell the Data Structure of your xml.

    And as there is only one DataTable in your DataSet, a DataTable object is enough.

    For example.

     

    Code Snippet

                DataSet data = new DataSet();

                data.ReadXmlSchema("Schema.xsd");

                dataGridView1.DataSource = data.Tables[0];

     

     

    and it can be output like

    Code Snippet

            void button_Click(object sender, EventArgs e)

            {

                DataTable dt = (DataTable)dataGridView1.DataSource;

                dt.WriteXml("result.xml");

            }

     

     

    The sample for XMLSchema

    Code Snippet

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

    <xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">

         <xs:element name="xmlTable">

             <xs:complexType>

                  <xs:sequence>

                       <xs:element name="ID" type="xs:int" />

                       <xs:element name="Name" type="xs:string" />

                       <xs:element name="Price" type="xs:string" />

                       <xs:element name="CreateTime" type="xs:dateTime" />

                  </xs:sequence>

             </xs:complexType>

             <xs:key name="xmlTableKey1">

                  <xs:selector xpath="." />

                  <xs:field xpath="mstns:ID" />

             </xs:key>

         </xs:element>

    </xs:schema>

     

     

    Hope this helps,

    Regards

    Wednesday, August 15, 2007 8:51 AM
  • Could you show me what changes you would make if you have more than one table?

     

     

    Thursday, August 16, 2007 9:35 AM