?
Solved

set DisplayMeber and ValueMember from XML using C#

Posted on 2008-10-07
8
Medium Priority
?
363 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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 900 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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