Solved

set DisplayMeber and ValueMember from XML using C#

Posted on 2008-10-07
8
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:Vivek Thangaswamy
ID: 22657516
i tried small also ... same result :(
0
 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 42
Code works but breaks when I add one section 4 29
Need to sort columns in DataGridView 4 39
Converting PHP triple des decrypt to c# 9 76
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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