Populating Combo from Data Table

  • Thread starter Thread starter Robert A. Boudra
  • Start date Start date
R

Robert A. Boudra

I would like to create a distinct list of last names from a data table,
then populate a combo box with these values. What's the best way to
accomplish this in ADO.net?

Bob
 
Hi Robert,

Create an dataadapter that retrieves distinct list.
Fill the DataTable using Fill method of DataAdapter.
Assing DataTable to ComboBox.DataSource method and set ComboBox.ValueMember.
 
Thanks for your help. Your instructions suggest that I should fill a
DataTable object directly from a DataAdapter. The examples I've seen say
that you fill a DataSet first from the DataAdapter, then create a DataTable
object from the Tables collection of the DataSet. Is there a way to fill a
DataTable object directly from the DataAdapter?

Bob
 
Hi Robert,

Robert A. Boudra said:
Thanks for your help. Your instructions suggest that I should fill a
DataTable object directly from a DataAdapter. The examples I've seen say
that you fill a DataSet first from the DataAdapter, then create a DataTable
object from the Tables collection of the DataSet. Is there a way to fill a
DataTable object directly from the DataAdapter?

Sure, just pass a DataTable instance to DataAdapter.Fill method.
There is no need for dataset whatsoever.
 
Robert, Miha's suggestion is a great way to do it, but let me add something.
If you bind it to the table you will have some problems if you try to add a
value to the control that doesn't belong to the table. A common one is
providing ALL value at the top and then each of the values beneath it where
ALL isn't in the datatable. In such a case, you can add ALL as a DataRow
and it will appear. Another issue is when you don't want every value in the
table. In that case, you can do either

foreach(DataRow dr in myTable.Rows){ comboBox.Items.Add(dr(Index);}
or for(int i = 0; i < myTable.Rows.Count-1; i++){
comboBox.Items.Add(myTable.Rows.[Index]
}

In general, the approach Miha suggests is the cleanest and easisest to
implement, but you can have some curve balls that might require a different
method.

HTH,

Bill
 
Hi William. When I add "All" to the datatable using the new datarow object,
how can I get All to be displayed at the top of the list?

Bob

William Ryan said:
Robert, Miha's suggestion is a great way to do it, but let me add something.
If you bind it to the table you will have some problems if you try to add a
value to the control that doesn't belong to the table. A common one is
providing ALL value at the top and then each of the values beneath it where
ALL isn't in the datatable. In such a case, you can add ALL as a DataRow
and it will appear. Another issue is when you don't want every value in the
table. In that case, you can do either

foreach(DataRow dr in myTable.Rows){ comboBox.Items.Add(dr(Index);}
or for(int i = 0; i < myTable.Rows.Count-1; i++){
comboBox.Items.Add(myTable.Rows.[Index]
}

In general, the approach Miha suggests is the cleanest and easisest to
implement, but you can have some curve balls that might require a different
method.

HTH,

Bill
Robert A. Boudra said:
I would like to create a distinct list of last names from a data table,
then populate a combo box with these values. What's the best way to
accomplish this in ADO.net?

Bob
 
Here's the code I'm using to try to fill the datatable. This is throwing an
error. What am I doing wrong?

Dim tblLNames As New DataTable
daLNames.Fill(tblLNames, "Addresses")
 
You don't need to pass a string since you are already passing a target
DataTable.
daLNames.Fill(tblLNames)
 
Back
Top