problem of combobox binding

tsh

Member
Joined
Jan 5, 2006
Messages
12
Programming Experience
Beginner
I am working in Visual studio 2003. I try to bind the combobox with a table from Microsoft Acess. I sucess to bind the GridData but fail in combobox. The combobox List not showing the correct data but show the word "System.Data.DataRowView" . Can someone help me abt this matter? thank
The codes is as below:

Dim myconnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=..\EdenSelect.mdb;")
Dim mydataadpater AsNew OleDbDataAdapter
Dim mydataset As DataSet = New DataSet
mydataadpater.SelectCommand = New OleDbCommand
mydataadpater.SelectCommand.Connection = myconnection
mydataadpater.SelectCommand.CommandText = "SELECT Field1 FROM Product WHERE CategoriesP='2'"
myconnection.Open()
mydataadpater.Fill(mydataset, "Product")
myconnection.Close()
grdData.DataSource = mydataset
grdData.DataMember = "Product"
ComboBox1.DataBindings.Add("SelectedItem", mydataset, "Product")
ComboBox1.DataBindings.Add("DataSource", mydataset, "Product")
ComboBox1.DataSource = mydataset
ComboBox1.DisplayMember = "Field1"
 
Hi,

A couple of things if i may say so...

First, to save you a bit of typing, you don't need to explicitly open and close the connection whilst call the fill command of the data adapter.
Also...
You don't need to add the databindings explicitly, because you have set the datasource property and it's display member propery. Try that, and if you are still getting the 'System.Data.DataRowView' then there may be a problem with you field name. so your code should look something like this...

VB.NET:
Dim myconnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=..\EdenSelect.mdb;")
Dim mydataadpater AsNew OleDbDataAdapter
Dim mydataset As DataSet = New DataSet
mydataadpater.SelectCommand = New OleDbCommand
mydataadpater.SelectCommand.Connection = myconnection
mydataadpater.SelectCommand.CommandText = "SELECT Field1 FROM Product WHERE CategoriesP='2'"
mydataadpater.Fill(mydataset, "Product")
grdData.DataSource = mydataset
grdData.DataMember = "Product"
ComboBox1.DataSource = mydataset
ComboBox1.DisplayMember = "Field1"
 
I think you'll find that you need to set the DisplayMember of the ComboBox to "Product.Field1" given that you are setting the DataSet as the DataSource. having said that, why use a DataSet at all? You're only using a single DataTable anyway so why not just use a DataTable without the DataSet? The DataSet serves no purpose whatsoever and only adds another layer of complexity. Many people seem to be obsessed with DataSets and forget that it's DataTables that store the data.
 
RE:problem of combobox binding

Thanks.....
But if don't add the databindings explicitly, i'll get the 'System.Data.DataViewManagerListItemTypeDescriptor' with single row only...
After add the databindings explicitly, i'll get the 'System.Data.DataRowView' with the total number of Field1 rows.
And i've check the field name, no problem with it....
Anything i need to check?
Thanks again.
 
Thanks.....
But if don't add the databindings explicitly, i'll get the 'System.Data.DataViewManagerListItemTypeDescriptor ' with single row only...
After add the databindings explicitly, i'll get the 'System.Data.DataRowView' with the total number of Field1 rows.
And i've check the field name, no problem with it....
Anything i need to check?
Thanks again.
 
prav_roy said:
i think so you need to bind combobox
ComboBox1.DisplayMember = "Field1"
combobox1.databind()
There is no DataBind method in WinForms.
 
When you set the datasource property of the combobox you are binding it to a datatable so there is no need to explicitly add the bindings in that manner.

Try this.....

VB.NET:
Combobox1.datasource = mydataset.tables("Product")
combobox1.displaymember = "Field1"

I must admit that the way you are doing this isn't perferable because you are not creating a datatable and adding it to the dataset. Some people woul say that this doesnt matter, but if you do create a datatable and name it the code is far more readable and easier to debug. You can also add the colums yourself and a primarykey, or you could just have the dataadapter do it for you by calling the fillschematable method before calling fill.
 
Back
Top