Link to home
Start Free TrialLog in
Avatar of Vivek Thangaswamy
Vivek ThangaswamyFlag for Australia

asked on

set DisplayMeber and ValueMember from XML using C#

Hi
I want to the displaymember and valuemember from XMLdocument to Combobox in Windows application

I am tring with the following code...
System.Collections.Generic.List<KeyValuePair<string, string>> ComBoData = new List<KeyValuePair<string, string>>();
                        ComBoData.Add(new KeyValuePair<string, string>(xElm.GetAttribute("Title").ToString(), xElm.GetAttribute("ID").ToString()));
                        ddl_List.DataSource = ComBoData;
                        ddl_List.DisplayMember = ?
                        ddl_List.ValueMember = ?

Open in new window

Avatar of Binuth
Binuth
Flag of India image

use "key" and "value"

like...
System.Collections.Generic.List<KeyValuePair<string, string>> ComBoData = new List<KeyValuePair<string, string>>();
                        ComBoData.Add(new KeyValuePair<string, string>(xElm.GetAttribute("Title").ToString(), xElm.GetAttribute("ID").ToString()));
                        ddl_List.DataSource = ComBoData;
                        ddl_List.DisplayMember = "value";
                        ddl_List.ValueMember = "key";

Open in new window

Avatar of Vivek Thangaswamy

ASKER

Hope this is correct
ddl_List.DisplayMember = "Key";
ddl_List.ValueMember = "Value";
But the result is nothing :(
even in ddl_List.text it has the value
 
small letter (k) and (v)
ddl_List.DisplayMember = "key";
ddl_List.ValueMember = "value";
i tried small also ... same result :(
or pls provide full code
it's working for me, can you post full code...
Chk this

 foreach (System.Xml.XmlElement xElm in xDoc.DocumentElement.ChildNodes)
            {
                if (xElm.Name.ToLower() == "list")
                {
                    try
                    {
                        System.Collections.Generic.List<KeyValuePair<string, string>> ComBoData = new List<KeyValuePair<string, string>>();
                        ComBoData.Add(new KeyValuePair<string, string>(xElm.GetAttribute("Title").ToString(), xElm.GetAttribute("ID").ToString()));
                        ddl_List.DataSource = ComBoData;
                        ddl_List.DisplayMember = "key";
                        ddl_List.ValueMember = "value";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Binuth
Binuth
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial