Populating a ComboBox in C#

A

Al Wilkerson

Hey,

I'm trying to populate a combBox in C# with values from an Access DB table.


myDataAdapter.Fill(myDS);

comboBox1.DataSource = myDS; // My dataSet
comboBox1.DataMember = "Compan Name"; // This is the column display
name
comboBox1.ValueMember = "CompanyName"; // This is the actual column name
that holds the values I want

Error message says: "Argument Exception: Could not bind to the new display
member. Parameter name: newDisplayMember"


What am I doing wrong ?

Thanks,
 
C

CT

Hi Al,

Inline, please.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105

Al Wilkerson said:
Hey,

I'm trying to populate a combBox in C# with values from an Access DB
table.


myDataAdapter.Fill(myDS);

comboBox1.DataSource = myDS; // My dataSet
comboBox1.DataMember = "Compan Name"; // This is the column display
name
comboBox1.ValueMember = "CompanyName"; // This is the actual column
name that holds the values I want

Try this:

ComboBox1.DataSource = myDS.Customers; // ...or whatever the table is
called
ComboBox1.DisplayMember = "Compan Name";
ComboBox1.ValueMember = "CompanyName";
 
A

Al Wilkerson

No, that doesn't work because you can't pass in the table like that.

Al

CT said:
Hi Al,

Inline, please.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105

Al Wilkerson said:
Hey,

I'm trying to populate a combBox in C# with values from an Access DB
table.


myDataAdapter.Fill(myDS);

comboBox1.DataSource = myDS; // My dataSet
comboBox1.DataMember = "Compan Name"; // This is the column display
name
comboBox1.ValueMember = "CompanyName"; // This is the actual column
name that holds the values I want

Try this:

ComboBox1.DataSource = myDS.Customers; // ...or whatever the table is
called
ComboBox1.DisplayMember = "Compan Name";
ComboBox1.ValueMember = "CompanyName";
 
J

james

I am not good at C# (and not much better in VB.NET) but, here is how I do that in VB.NET:

' connects to main client list BondCXR

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" & mypath() &
"\Data\BondCXR.mdb;Mode=ReadWrite")

conn.Open()

Try

Catch oledbe As OleDbException

MessageBox.Show(oledbe.Message)

Finally

End Try

If conn.State.ToString() = "Open" Then

comm = New OleDbCommand("Select * from Row Order by [LAST]", conn)

da = New OleDbDataAdapter(comm)

ds = New DataSet("Row")

da.Fill(ds, "Row")

DataGrid1.SetDataBinding(ds, "Row")

' bmb is the Binding Manager

bmb = Me.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)

Dim cm As Integer

cm = ds.Tables(0).Rows.Count - 1

Dim rowCount As Integer = cm

'assumes datasource is a datatable...

Dim colCount As Integer

Dim row As Integer

For row = 0 To rowCount - 1

Dim cellValue As Object = Me.DataGrid1(row, 2) + Me.DataGrid1(row, 3)

ComboBox1.Items.Add((cellValue.ToString() + " "))

Next row

End If

conn.Close()

Me.Text = conn.DataSource.ToString

ComboBox1.Visible = True

End Sub

I think you get the idea. You have to know the number of rows in your datasource. In my case I am using the Datagrid as the
source to populate the combobox. And then you have to step thru each row at the column you want and extract the value at the
cell you are interested in and Add that to your combobox.
Hope this gives you the general idea.
(sorry it's not a C# sample)
james
 
S

Sylvain Lafontaine

The DataMember should not be a column but a table (or a view). Here the
piece of code that I'm using for a statement like "Select IdOrganisme, Name
from Organismes" :

DataSet ds = new DataSet();

myAdapter.Fill (ds, "Organismes"); // The name of the table is set
explicitly to « Organismes » here.


ComboIdOrganisme.DataSource = ds;

ComboIdOrganisme.DataMember = "Organismes";

ComboIdOrganisme.DataValueField = "IdOrganisme";

ComboIdOrganisme.DataTextField = "Name";

// ComboIdOrganisme.DataTextFormatString = "{0}"; // In case you want some
formatting for your display.

ComboIdOrganisme.DataBind();

.....

Of course, by using statements like « ComboIdOrganisme.DataSource =
ds.Tables ["Organismes"]; »; some of the above statement are no longer
required. You can also populate your combobox explicitly; for exemple:

while (sqlReader1.Read ()) {

ComboIdOrganisme.Items.Add (new ListItem (sqlReader1
["Name"].ToString(), sqlReader1["IdOrganisme"].ToString()));

}

S. L.
 
A

Al Wilkerson

Thank you all, but I figured it out. This worked for me:

ComboBox1.DataSource = myDS;
ComboBox1.DisplayMember = "Company Name"; // Column Display
Name
ComboBox1.ValueMember = "Suppliers.CompanyName"; // tablename.column name

Al

Sylvain Lafontaine said:
The DataMember should not be a column but a table (or a view). Here the
piece of code that I'm using for a statement like "Select IdOrganisme,
Name from Organismes" :

DataSet ds = new DataSet();

myAdapter.Fill (ds, "Organismes"); // The name of the table is set
explicitly to « Organismes » here.


ComboIdOrganisme.DataSource = ds;

ComboIdOrganisme.DataMember = "Organismes";

ComboIdOrganisme.DataValueField = "IdOrganisme";

ComboIdOrganisme.DataTextField = "Name";

// ComboIdOrganisme.DataTextFormatString = "{0}"; // In case you want
some formatting for your display.

ComboIdOrganisme.DataBind();

....

Of course, by using statements like « ComboIdOrganisme.DataSource =
ds.Tables ["Organismes"]; »; some of the above statement are no longer
required. You can also populate your combobox explicitly; for exemple:

while (sqlReader1.Read ()) {

ComboIdOrganisme.Items.Add (new ListItem (sqlReader1
["Name"].ToString(), sqlReader1["IdOrganisme"].ToString()));

}

S. L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top