none
Binding a DataGridView to a typed DataSet filled with XML data RRS feed

  • Question

  • If I fill an untyped DataSet with XML data using

    Code Snippet
    dataSet.ReadXml( "regions.xml" );

    the DataGridView bound to this DataSet shows columns and data correctly. But I needed to use a typed DataSet in order to have design-time features for the DataGridView, so I made a typed DataSet and added a single DataTable to it (I mean not all elements of the XML file are mapped to DataTables, just one element), then I filled the new typed DataSet with XML data using the same line of code above, then the DataSet is NOT loaded with data (i.e. dataSet.Tables[0].Rows.Count = 0)!!! I thought I had to make sure that datatypes of the XML data should be equal/convertible to datatypes of DataTable's columns, I did but the problem still exists!

     

    Can anyone help?

    Sunday, June 22, 2008 6:49 AM

Answers

  •  

    Hi Ahmed Mostafa,

    Based on my experience, the reason why you get no record is because of the different namespace of the xml file and the typed DataSet. I have the following xml file

    Code Snippet
     <MyDataSet xmlns="MyDataSetnamespace">

      <Order_Details>

        <OrderID>10248</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

      <Order_Details>

        <OrderID>10249</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

      <Order_Details>

        <OrderID>10250</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

    </MyDataSet>

     

    And the a typed DataSet with a table named Order_Details, and the table has only one column with the name of OrderID. I use the following code to read the xml file the Typed DataSet instance and it worked well on my machine.

    Code Snippet

                MyDataSet myds = new MyDataSet();

                myds.Namespace = "MyDataSetnamespace";

                myds.ReadXml("myxml.xml");

                this.dataGridView2.DataSource = myds.Tables[0];

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Friday, June 27, 2008 7:11 AM

All replies

  •  

    Hi Ahmed Mostafa,

    Based on my experience, the reason why you get no record is because of the different namespace of the xml file and the typed DataSet. I have the following xml file

    Code Snippet
     <MyDataSet xmlns="MyDataSetnamespace">

      <Order_Details>

        <OrderID>10248</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

      <Order_Details>

        <OrderID>10249</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

      <Order_Details>

        <OrderID>10250</OrderID>

        <ProductID>11</ProductID>

        <UnitPrice>14.0000</UnitPrice>

        <Quantity>12</Quantity>

        <Discount>0</Discount>

      </Order_Details>

    </MyDataSet>

     

    And the a typed DataSet with a table named Order_Details, and the table has only one column with the name of OrderID. I use the following code to read the xml file the Typed DataSet instance and it worked well on my machine.

    Code Snippet

                MyDataSet myds = new MyDataSet();

                myds.Namespace = "MyDataSetnamespace";

                myds.ReadXml("myxml.xml");

                this.dataGridView2.DataSource = myds.Tables[0];

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Friday, June 27, 2008 7:11 AM
  • Thanks Rong-Chun Zhang,

    It worked as you said, but what about child elements? how can I know that some DataRow in the DataSet is the parent of some DataRows in another table? For example, for this XML file:

     

    Code Snippet

    <Interface>

    <Name>eth0</Name>

    <Index>1</Index>

    <IP>10.0.0.2</IP>

    <Group>

    <IP>224.4.5.6</IP>

    <Mode>Include</Mode>

    <Source>

    <IP>10.0.0.7</IP>

    </Source>

    <Source>

    <IP>10.0.0.3</IP>

    </Source>

    <Source>

    <IP>10.0.0.8</IP>

    </Source>

    <Source>

    <IP>10.0.20.5</IP>

    </Source>

    </Group>

    <Group>

    <IP>224.6.5.4</IP>

    <Mode>Exclude</Mode>

    <Source>

    <IP>10.0.26.5</IP>

    </Source>

    </Group>

    </Interface>

     

    how to get GroupRow[] that are children of some InterfaceRow?

     

    PS: I need to use the DataSet designer to represent the relation, and I don't know how to add the relation without the foreign key, because the foreign key is not an element in the XML file.


    Thanks a lot

    Sunday, June 29, 2008 8:20 AM