Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell me when i populate data to combobox from xml i will be loosing all the previous data & displays only the bounded data. I want to read data from xml & display in combobox, something like display saved data.

My code is as shown below:

C#
string dsFileName = "tst1.xml";
DataSet dsFeeds = new DataSet();
dsFeeds.ReadXml(dsFileName);
stg1vtgtypcmbbox.DataSource = dsFeeds.Tables[0];
stg1vtgtypcmbbox.DisplayMember = "VOLTAGETYPE";
stg1vtgtypcmbbox.ValueMember = "VOLTAGETYPE";


My xml file contains data like shown below:

XML
<tst2>
<STAGE1>
<VOLTAGETYPE>
40
</VOLTAGETYPE>
<OP_VAL>
101
</OP_VAL>
<DEF_INV>
YES
</DEF_INV>
<OP_TIME>
0.00
</OP_TIME>
<RST_VAL>
85
</RST_VAL>
<RST_TIME>
0.00
</RST_TIME>
</STAGE1>
</tst2>


But during load event the stg1vtgcmbbox contains values like 20, 40, 65.. After doing the above operation the combobox is cleared & only 40 is displayed. This shouldn't happen, i want the previous values also to be present & display the value that is read from xml file.
Posted

1 solution

Here is some sample XML I'm using for this solution.

XML
<?xml version="1.0" encoding="utf-8"?>
<data>
  <comboBox id="comboBox1">
    <item displayName="ComboBox1 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox1 Test 2" value="ComboBox1 Test 2" selected="true" />
    <item displayName="ComboBox1 Test 3" value="ComboBox1 Test 3" selected="false" />
    <item displayName="ComboBox1 Test 4" value="ComboBox1 Test 4" selected="false" />
    <item displayName="ComboBox1 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
  <comboBox id="comboBox2">
    <item displayName="ComboBox2 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox2 Test 2" value="ComboBox1 Test 2" selected="false" />
    <item displayName="ComboBox2 Test 3" value="ComboBox1 Test 3" selected="true" />
    <item displayName="ComboBox2 Test 4" value="ComboBox1 Test 4" selected="false" />
    <item displayName="ComboBox2 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
  <comboBox id="comboBox3">
    <item displayName="ComboBox3 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox3 Test 2" value="ComboBox1 Test 2" selected="false" />
    <item displayName="ComboBox3 Test 3" value="ComboBox1 Test 3" selected="false" />
    <item displayName="ComboBox3 Test 4" value="ComboBox1 Test 4" selected="true" />
    <item displayName="ComboBox3 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
</data>


So the first step is to load the data from the XML file and bind it to the ComboBox objects on the form.

C#
private void loadButton_Click(object sender, EventArgs e)
{
    var comboBoxData = XDocument.Load("ComboBoxData.xml");

    foreach (var element in comboBoxData.Element("data").Elements("comboBox"))
    {
        ComboBox comboBox =
            (ComboBox)(Controls.Find(element.Attribute("id").Value, false)).First();

        comboBox.DataSource =
            (from data in element.Elements("item")
             select new ComboBoxItem
             {
                 ItemDisplayName = data.Attribute("displayName").Value,
                 ItemValue = data.Attribute("value").Value
             }).ToList();

        comboBox.DisplayMember = "ItemDisplayName";
        comboBox.ValueMember = "ItemValue";

        comboBox.SelectedValue =
            (from data in element.Elements("item")
             where data.Attribute("selected").Value == "true"
             select data.Attribute("value").Value).First();
    }
}



I use an XDocument to load the data from the file. If your files get too large, this won't be an efficient way to process this data. Next step is to loop through all of the ComboBox objects on the form. Using the ID of the control stored in the XML file, I'm able to find the control on the form and work with it directly. I use LINQ projection to build object list that I can bind to the ComboBox. I set up the display name and value members and I also select the item to display where the attribute "selected" has "true".

Now all of your ComboBox objects are bound to the XML file, the next fun part is to save their state.

C#
private void saveButton_Click(object sender, EventArgs e)
{
    var comboBoxData = new XDocument();
    var dataRootElement = new XElement("data");

    foreach (Control control in this.Controls)
    {
        if (control is ComboBox)
        {
            var comboBoxElement = new XElement("comboBox");
            comboBoxElement.SetAttributeValue("id", control.Name);

            for (int index = 0; index < ((ComboBox)control).Items.Count; index++)
            {
                ComboBoxItem item =
                    (ComboBoxItem)((ComboBox)control).Items[index];

                var itemElement = new XElement("item");
                itemElement.SetAttributeValue("displayName", item.ItemDisplayName);
                itemElement.SetAttributeValue("value", item.ItemValue);
                itemElement.SetAttributeValue("selected",
                    ((ComboBox)control).SelectedIndex == index ? "true" : "false");

                comboBoxElement.Add(itemElement);
            }

            dataRootElement.Add(comboBoxElement);
        }
    }

    comboBoxData.Add(dataRootElement);
    comboBoxData.Save("ComboBoxData.xml");
}


This is actually easier then it sounds. I use the XDocument and XElement objects to construct each XML element for the document. I use attributes to store the state of the object.

Hopefully this gives you a good start!
 
Share this answer
 
v2
Comments
Jagadisha_Ingenious 9-Aug-13 2:52am    
@virusstorm: Thanks for the suggestion u have given. But the actual stuff is i have many comboboxes to which the data is added manually. While loading the form these values are loaded with the selected index to show @ startup. I have 2 buttons save parameters & another load parameters, so during save i am fetching the selecteditem from all the comboboxes, creating a xml file and saving it in a local directory. When i click on load parameters i need to fetch & display the parameters that was saved earlier. So in this case how to do it...?
virusstorm 9-Aug-13 8:35am    
Just making sure I understand correctly. You have say 5 ComboBox objects on your form. You hit save, you are capturing what is in each ComboBox and the selected index?
Jagadisha_Ingenious 11-Aug-13 0:47am    
@Virusstorm: Yup thats exactly what i am doing... Then when i click load button & select the saved file i need to show the values that was saved in those ComboBoxes.
virusstorm 13-Aug-13 13:34pm    
I deviated from your XML document, but it was easier for me to demo how this would work if I kept the XML simple. Hopefully this gives you a good starting point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900