Hey i would like to know how i can use a combobox to show alternatives which i will choose from but with more information when i choose

like if a combobox has theese options

Klas
Maria
Mat

i choose Mat and the selected index is Mat

now i want it something like this

male - Klas - group1
female - Maria - group2
male - Mat - group1

i choose Maria and the selected index is Maria
so i dont have to cut the string up and divide and stuff can you use some kind of listview? how?
Thank you for answers
Dendei.

Recommended Answers

All 23 Replies

The DisplayMember and ValueMember settings of the combo-box allow you to make the display value different to the selected one.

mhm yes thank you, i've been looking around some.
anyone know a good guide about this comboboxes and datatables linked to it and displayMember and ValueMember?
how do i fill in the alternatives? and stuff?

In my experience with listboxes (they use the same fields)

You bind the datatable as the datasource of the combobox, then give the column names for the displaymember and valuemember as required.

still dont know how to start xD anyone have a decent guide on this?

Let me see if I can find some old code I've written..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DaniWebWinForm
{
    public partial class Form1 : Form
    {
        DataSet DS = new DataSet();
        bool FormLoading = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormLoading = true;
            DS.Tables.Add("NameTable"); //Setup table holding names

            DS.Tables["NameTable"].Columns.Add("NameID"); // Primary key
            DS.Tables["NameTable"].Columns.Add("Name");
            DS.Tables["NameTable"].Columns.Add("Age");

            DS.Tables["NameTable"].Rows.Add(1, "Mike Jobs", "23");
            DS.Tables["NameTable"].Rows.Add(2, "Jack Starling", "24");
            DS.Tables["NameTable"].Rows.Add(3, "Sam Eker", "16");
            DS.Tables["NameTable"].Rows.Add(4, "Dani West", "20");
            DS.Tables["NameTable"].Rows.Add(5, "Steve Marvin", "31");

            comboBox1.DataSource = DS.Tables["NameTable"];
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "NameID";
            FormLoading = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!FormLoading)
                MessageBox.Show(comboBox1.SelectedValue.ToString());
        }


    }
}

Simple WinForms App using one combo box.

Everytime you select a name from the combo box it will display the NameID field value related to that name. Hope that helps you understand :)

yes thank you easy to follow and nice :)

tested your code but should'nt the age of the people be showing aswell in the combobox? and is there a way yo show the colums over

name age
mike jobs 23

? :)

Nope the combo box was set to display the name only using the line: comboBox1.DisplayMember = "Name";, I added the Age field simply so I didnt just make two columns and assign each to a property.

Give me a couple of mins to have a play and I'll see :)

hehe tried to just comboBox1.DisplayMember = "Name" + "age"; and stuff but nono :P
but yea would be nice to be able to show more so the user gets more information before choosing, and when they choose get the "NameID" to work with

Yeap I've tried that too! :p

seems like comboBox1.DisplayMember only takes one member :/ maybe need to make different Displaymembers somehow? :D

comboBox1.DisplayMember = DS.Tables["NameTable"].Columns.ToString(); dont work
I dont think im getting closer xD

Hmm it all depends on whether or not your able to edit the DataTables as to how you approach this.

Its either add a new column to the datatable which contains a concatenation of the data you wish to display or write a custom class that overrides the combobox. Which I know there is code for on google because I've just looked at it :)

i guess i can
string Display = name + " - " + age + " - " + gender;
DS.Tables["NameTable"].Columns.Add(Display);
something like that? since it just display anyway i can do what ever and use
valuemember in my other code :)

hmm have to go but i will continue look into this soon, thank you for all help MikeyIsMe!

Yeap can do it exactly that way. Adaption to my code above would appear as the following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DaniWebWinForm
{
    public partial class Form1 : Form
    {
        DataSet DS = new DataSet();
        bool FormLoading = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormLoading = true;
            DS.Tables.Add("NameTable"); //Setup table holding names

            DS.Tables["NameTable"].Columns.Add("NameID"); // Primary key
            DS.Tables["NameTable"].Columns.Add("Name");
            DS.Tables["NameTable"].Columns.Add("Age");

            DS.Tables["NameTable"].Rows.Add(1, "Mike Jobs", "23");
            DS.Tables["NameTable"].Rows.Add(2, "Jack Starling", "24");
            DS.Tables["NameTable"].Rows.Add(3, "Sam Eker", "16");
            DS.Tables["NameTable"].Rows.Add(4, "Dani West", "20");
            DS.Tables["NameTable"].Rows.Add(5, "Steve Marvin", "31");

            // Adding the new column and programatically assigning the value
            DS.Tables["NameTable"].Columns.Add("ConCatInfo", typeof(string), "Name + ' | ' + Age");

            comboBox1.DataSource = DS.Tables["NameTable"];
            comboBox1.DisplayMember = "ConCatInfo";
            comboBox1.ValueMember = "NameID";
            FormLoading = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!FormLoading)
                MessageBox.Show(comboBox1.SelectedValue.ToString());
        }


    }
}

With regards to displaying the column headers above, you can just use a label and space out the entries :)

getting it to work somehow now :) got stuck forever because needed to fix the text order abit :D

comboBox1.DataSource = DS.Tables["NameTable"];
comboBox1.DisplayMember = "ConCatInfo";
comboBox1.ValueMember = "NameID";

the program didnt like when you created the Datasource link to the box before setting the Display and Value member first :)
so works nice now with

comboBox1.DisplayMember = "ConCatInfo";
comboBox1.ValueMember = "NameID"; 
comboBox1.DataSource = DS.Tables["NameTable"];

MikeyIsMe - "With regards to displaying the column headers above, you can just use a label and space out the entries :)"

But if i have different long names and such it will look like crap? :D maybe i can do a row that cant be selected as headers somehow?

Ummm.. It would look odd yes.

I dont know of a way to specifically add a header entry.

been looking at this link Click Here
but got very complex :/ and the demo project didnt work for me that they posted :/

Yeap I've seen that link also, it is pretty complex. Hence why I didnt look that much into it :)

mhm yea :P well it would look good but dont know if its worth the truble at this point xD but i guess if i want to take it to the next step, that is the way to go :)
thank you again for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.