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

  • Home
  • SEARCH
  • 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
  • 1 View
  • 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

Related Questions

I have an app that would benefit from google-style autocompletion while filling in an
Often I find myself filling ASP.NET repeaters with items that need the CSS class
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
In some library code, I have a List that can contain 50,000 items or
I need to fill a object using jQuery I have a dialog (jQueryUI) that
I am filling up a Big Table with text items from different countries. My
I am writing windows from application in that i am using combo box control.
I'm parsing an XML file and filling a table view. The XML has a
I have a site that directs a user to a php page, after filling
I have a simple VB.NET 2008 app that helps users to edit fields in

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.