Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 194155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:34:16+00:00 2026-05-11T16:34:16+00:00

I am filling items to my ComboBox from a XML file using a DataTable.

  • 0

I am filling items to my ComboBox from a XML file using a DataTable. Currently I have it set so that one column is ComboBox’s displaymember and another is it’s value member. However this may not always work for me, since I have to set the selectedItem parameter, and value member may not be unique.

I don’t know if there is a duplicate of the value member in the table or not, so my idea was that I would put entire DataRow as the value member of the ComboBox and then use ComboBox.SelectedITem = (DataRow)some_data_row; for selecting, and it would always select the right ComboBox object.

How would I accomplish this? IS there a better way of doing this? I’m open to suggestions, however it is very important that I can get to both, display member and value member.

Thank you for your help!

EDIT: Maybe I wasn’t clear enough before, however while I am asking if this is the best approach here, I am also asking how to do this. If I don’t set the valuemember parameter, the SelectedItem is of DataRowView type… Please note, that I want to use the selectedValue parameter to select items from ComboBox, and if I try to do that without explicitly setting the value member an exception is thrown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:34:16+00:00Added an answer on May 11, 2026 at 4:34 pm

    First of all thank you Adam Robinson, I’m sure your answer was correct, but it just wasn’t what I wanted to hear. I solved my problem in a different way and I think it may be useful to someone else, so I am posting it here.

    What I did was I created a new class, in my case I named it ListObject, which had a property DataRow (as you will see later it works for other types too, I just used this since this is what I actually wanted as my Item value property). It also overrides methods:

    • String ToString()
    • bool Equals(object obj)
    • int GetHashCode() –is not needed in my case, however Visual Studio
      warns you it should be overridden.

    The idea was that I could fill ComboBox.Items collections with objects of my own class, display a custom string (if I had not worked it out like this, my next question on Stack overflow would probably be about customizing DisplayMembers when reading items from a DataRow) and compare only one class’s item (in my case DataRow).

    So here is the code and it works great (at least for what I wanted to do with it).

    public class ListObject
    {
        public DataRow element;
    
        public String DisplayObject = null;
    
        public ListObject(DataRow dr)
        {
            element = dr;
        }
    
        public ListObject(DataRow dr, String dspObject)
        {
            element = dr;
            DisplayObject = dspObject;
        }
    
        public override String ToString()
        {
            if (DisplayObject == null) throw new Exception("DisplayObject property was not set.");
    
            return element[DisplayObject].ToString();
        }
    
        public override bool Equals(object obj)
        {
            if (obj.GetType() == typeof(ListObject))
                return Equals(((ListObject)obj).element, this.element);
            else return base.Equals(obj);
        }
    
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
    

    In my case it works great since I can just fill the ComboBox’s with a foreach statement:

    dtUsers.ReadXml(Program.Settings.xmlInputUsers);
    
    foreach(DataRow dr in dtUsers.Rows) 
    {
        cmbUser.Items.Add(new ListObject(dr, "Name"));
    }
    

    And when I get the DataRow I want selected I just do this:

    cmbUser.SelectedItem = new ListObject(dlg.SelectedDataRow);
    

    Where I don’t have to worry about the DisplayMember etc, because only DataRow’s will be compared, and your display parameters will still be set from when you filled ComboBox.Items collection. Also since toString method is overridden you can really customize your output.

    Creating this class was only possible because of msdn article on ComboBox.SelectedItem Property in which it was noted, that SelectedItem property works using the IndexOf method. This method uses the Equals method to determine equality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming that Trip is an IQueryable, a fluent interface could… May 11, 2026 at 8:28 pm
  • Editorial Team
    Editorial Team added an answer You can just have the parent VM connect to the… May 11, 2026 at 8:28 pm
  • Editorial Team
    Editorial Team added an answer The way your relationship is set up, each image as… May 11, 2026 at 8:28 pm

Related Questions

I have a scenario where I have multiple threads adding to a queue and
I have a winform data entry and retrieval program called CaseNotes. This has a
I am looking for a library that can be used in .NET to retrieve
I am writing this java program to find all the prime numbers up to
The answer to this question may turn out to be, Don't use typed DataSets

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.