Solved

set DisplayMeber and ValueMember from XML using C#

Posted on 2008-10-07
8
324 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Vivek Thangaswamy
  • 5
  • 3
8 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22657455
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

0
 
LVL 11

Author Comment

by:Vivek Thangaswamy
ID: 22657501
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
 
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22657509
small letter (k) and (v)
ddl_List.DisplayMember = "key";
ddl_List.ValueMember = "value";
0
 
LVL 11

Author Comment

by:Vivek Thangaswamy
ID: 22657516
i tried small also ... same result :(
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:Binuth
ID: 22657521
or pls provide full code
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22657546
it's working for me, can you post full code...
0
 
LVL 11

Author Comment

by:Vivek Thangaswamy
ID: 22657553
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

0
 
LVL 14

Accepted Solution

by:
Binuth earned 300 total points
ID: 22657576
hmm... problem was you are trying to create new object ComBoData in every loop, so it always contains one value

change the code like...
try

	{
 

		System.Collections.Generic.List<KeyValuePair<string , string>> ComBoData = new List<KeyValuePair<string , string>>();

		foreach (System.Xml.XmlElement xElm in xDoc.DocumentElement.ChildNodes)

		{

			if (xElm.Name.ToLower() == "list")

			{

				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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now