Click here to Skip to main content
15,902,635 members
Home / Discussions / C#
   

C#

 
AnswerRe: Deploying Database Pin
Alex Casals23-Jan-09 23:32
professionalAlex Casals23-Jan-09 23:32 
QuestionC# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01223-Jan-09 9:13
Wheels01223-Jan-09 9:13 
AnswerRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM6923-Jan-09 9:16
professionalDaveyM6923-Jan-09 9:16 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01223-Jan-09 9:29
Wheels01223-Jan-09 9:29 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM6923-Jan-09 10:03
professionalDaveyM6923-Jan-09 10:03 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01226-Jan-09 1:32
Wheels01226-Jan-09 1:32 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01228-Jan-09 2:05
Wheels01228-Jan-09 2:05 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM6928-Jan-09 7:08
professionalDaveyM6928-Jan-09 7:08 
There's no need to add your items in a loop, just assign the combo's DataSource to the DataSet's DataTable (ds.Tables[0]) and set the DisplayMember and ValueMember to the table's respective ColumnNames and that should fill the combo.

To get the value just get the combobox's SelectedValue. Working example below Smile | :)
void InitializeCombo()
{
    // Constructing data set in code, obviously this would come from your DAL
    DataSet ds = new DataSet();
    ds.Tables.Add();
    ds.Tables[0].Columns.Add("ID");
    ds.Tables[0].Columns.Add("Display");
    ds.Tables[0].Rows.Add(new object[] { 1, "One" });
    ds.Tables[0].Rows.Add(new object[] { 2, "Two" });
    ds.Tables[0].Rows.Add(new object[] { 3, "Three" });
    ds.Tables[0].Rows.Add(new object[] { 4, "Four" });
    ds.Tables[0].Rows.Add(new object[] { 5, "Five" });
    // the next three lines fill your combo and set the members
    comboBox1.DataSource = ds.Tables[0];
    comboBox1.DisplayMember = "Display";
    comboBox1.ValueMember = "ID";
    // subscribe to SelectedIndexChanged event
    comboBox1.SelectedIndexChanged += new EventHandler(
        comboBox1_SelectedIndexChanged);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(comboBox1.SelectedValue.ToString());
}
P.S. There's no need for all the x.ToString()s that you have everywhere!

Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01228-Jan-09 8:37
Wheels01228-Jan-09 8:37 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM6928-Jan-09 9:41
professionalDaveyM6928-Jan-09 9:41 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM6930-Jan-09 9:28
professionalDaveyM6930-Jan-09 9:28 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels01230-Jan-09 9:31
Wheels01230-Jan-09 9:31 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels0124-Feb-09 2:47
Wheels0124-Feb-09 2:47 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
Wheels0124-Feb-09 8:08
Wheels0124-Feb-09 8:08 
GeneralRe: C# Populate Multi column (2) combobox with existing dataset (hide record #) Pin
DaveyM694-Feb-09 13:16
professionalDaveyM694-Feb-09 13:16 
QuestionSplash Screen / Init question Pin
EliottA23-Jan-09 8:50
EliottA23-Jan-09 8:50 
AnswerRe: Splash Screen / Init question Pin
DaveyM6923-Jan-09 9:10
professionalDaveyM6923-Jan-09 9:10 
AnswerRe: Splash Screen / Init question Pin
User 665823-Jan-09 10:01
User 665823-Jan-09 10:01 
QuestionError: RegularExpression can't be found [modified] Pin
u060509423-Jan-09 8:35
u060509423-Jan-09 8:35 
AnswerRe: Error: RegularExpression can't be found Pin
DaveyM6923-Jan-09 8:41
professionalDaveyM6923-Jan-09 8:41 
GeneralRe: Error: RegularExpression can't be found Pin
u060509423-Jan-09 8:43
u060509423-Jan-09 8:43 
GeneralRe: Error: RegularExpression can't be found Pin
DaveyM6923-Jan-09 8:51
professionalDaveyM6923-Jan-09 8:51 
GeneralRe: Error: RegularExpression can't be found Pin
u060509423-Jan-09 8:57
u060509423-Jan-09 8:57 
GeneralRe: Error: RegularExpression can't be found Pin
DaveyM6923-Jan-09 9:06
professionalDaveyM6923-Jan-09 9:06 
GeneralRe: Error: RegularExpression can't be found Pin
PIEBALDconsult23-Jan-09 9:43
mvePIEBALDconsult23-Jan-09 9:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.