How to read xml to combobox in datagrid?

Viewed 352

I am trying to read xml file to dataset, and binding the dataset to datagrid

XML file:

<NewDataSet>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>BaudRate</ParamName>
        <ParamValues>
            <ParamValue>9600</ParamValue>
            <ParamValue>19200</ParamValue>
            <ParamValue>115200</ParamValue>
        </ParamValues>
        <DefaultValue>19200</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>Parity</ParamName>
        <ParamValues>
            <ParamValue>None</ParamValue>
            <ParamValue>Odd</ParamValue>
            <ParamValue>Even</ParamValue>
        </ParamValues>
        <DefaultValue>None</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
        <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>StopBit</ParamName>
        <ParamValues>
           <ParamValue>1</ParamValue>
           <ParamValue>2</ParamValue>
        </ParamValues>
        <DefaultValue>1</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>DataBit</ParamName>
        <ParamValues>
           <ParamValue>7</ParamValue>
           <ParamValue>8</ParamValue>
        </ParamValues>
        <DefaultValue>8</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
   </Communications>
   <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>SlaveAddress</ParamName>
        <ParamValues>
            <ParamValue>1</ParamValue>
        </ParamValues>
        <DefaultValue>1</DefaultValue>
        <MaxValue>247</MaxValue>
        <MinValue>1</MinValue>
   </Communications>
</NewDataSet>

Read xml to Dataset:

    public ObservableCollection<Communication> GetCommunications()
    {
        DataSet ds = new DataSet();
        ds.ReadXml("Communications.xml");
        ObservableCollection<Communication> communications = new ObservableCollection<Communication>();
        foreach (DataRow communicationRow in ds.Tables["Communications"].Rows)
        {
            var c = new Communication((ushort)Convert.ToInt16(communicationRow["ModelNumber"]), communicationRow["ParamName"].ToString(),
                    ds.Tables["ParamValue"].Rows[0][0].ToString(), communicationRow["DefaultValue"].ToString(), communicationRow["MaxValue"].ToString(),
                    communicationRow["MinValue"].ToString());
            foreach (DataRow dr in ds.Tables["ParamValue"].Rows)
            {
                c.ParamValues.Add(dr[0].ToString());
            }
            communications.Add(c);
        }
        return communications;
    }

Binding Datagrid:

    <DataGridTemplateColumn Header="ParamValues">                                              
       <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                 <ComboBox ItemsSource="{Binding ParamValues}" SelectedItem="{Binding DefaultValue}" />
            </DataTemplate>                                                           
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

But the above code runs with bug that every column of combobox ParamValues has all the ParamValue of every table Communications Image detail, could you please tell me how to fix this?Thanks in advance!

2 Answers
Related