Hello Everyone,

I have a database that has some items and I would like to fill a combo box with the items and have the combobox selected value set the corresponding item ID. I have seen so many ways to do this I am not sure which way to go. Any help is appreciated. This is what I have so far:

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;

namespace DBApp
{
    public partial class Form1 : Form
    {
        DB db;
        public Form1()
        {
            DataSet ds;
            db = new DB();
            ds = db.getRecords();
            InitializeComponent();
            
    
            comboBox1.DataSource = ds;
            comboBox1.DisplayMember = "PlatformName";
            comboBox1.ValueMember = "PlatformID";
        }
    }
}

With the code above an exception gets thrown:

The error is:
Cannot bind to the new display member.
Parameter name: newDisplayMember

Thanks.

Recommended Answers

All 2 Replies

This is your post no 4th. You haven't follow the rules; How to place source? Please read http://www.daniweb.com/forums/announcement9-3.html.

Source code must be surrounded with CODE tags.

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;

namespace DBApp
{
public partial class Form1 : Form
       {
             DB db;
             public Form1()
                {
                    DataSet ds;
                    db = new DB();
                    ds = db.getRecords();
                    InitializeComponent();


                   comboBox1.DataSource = ds.Tables["tablename"];
                   comboBox1.DisplayMember = ds.Tables["tablename"].Columns[0].ColumnName;
                   comboBox1.ValueMember =ds.Tables["tablename"].Columns[1].ColumnName;
                }
       }
}

Please send the full code, getRecords ??

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.