Problem using Bindingsource - shows System.Data.DataRowView instead of Displaymember

PatM

Well-known member
Joined
Dec 5, 2012
Messages
52
Programming Experience
10+
I have a Dataset and Datatable (which is typed but empty when I assign the bindingsource) and I'm trying to get that to show in a combobox. Here's the relevent sections:

On the form I added BindingSource1

Declarations
VB.NET:
 Dim dsetMySet As DataSet = New DataSet("MySet")
 Dim dtMyTable1 As DataTable = New DataTable("MyTable1")

The table:
VB.NET:
            dtMyTable1.Columns.Add("Serial", GetType(System.Int32))
            dtMyTable1.Columns("Serial").Caption = "Serial #"

            dtMyTable1.Columns.Add("Name", GetType(System.String))
            dtMyTable1.Columns("Name").Caption = "Name"

            dtMyTable1.Columns.Add("ListEntry", GetType(System.String))
            dtMyTable1.Columns("ListEntry").Caption = "Oops"
            dtMyTable1.Columns("ListEntry").Expression = "Serial + ' ' + Name"

            kprimarykey(0) = dtMyTable1.Columns("Serial")
            dtMyTable1.PrimaryKey = kprimarykey

            dsetMySet.Tables.Add(dtMyTable1)

Then the bindingsource and combobox stuff
VB.NET:
        BindingSource1.DataMember = "MyTable1"
        BindingSource1.DataSource = dsetMySet


        ComboBox1.DisplayMember = "ListEntry"
        'ComboBox1.ValueMember = "Serial"
        ComboBox1.DataSource = BindingSource1

When I fill the dataset I get System.Data.DataRowView instead of the ListEntry text. If I assign the table itself as the datasource then I get the proper ListEntry text.

I've tried different ordering of the assigmnents and googled all morning but I can't see where I'm going wrong.
 
Is this just a test scenario? If not then just don't use a DataSet because a DataSet containing a single DataTable is all but useless anyway. If an untyped DataSet doesn't contain multiple tables then it's completely pointless and even if there are multiple DataTables, if the number is small and there's no DataRelations between them then I'd still probably use discrete DataTables.

That said, while I haven't tested, I'm guessing that, because the original data source is a DataSet, you need to qualify your column names with the table name, e.g.
VB.NET:
ComboBox1.DisplayMember = "MyTable1.ListEntry"
 
The actual program has 8 tables with relations (so far). It works when I use a dataset and bindings on the form but the table editor sucks in that you can't insert or reorder columns. Makes it a pain.

Forgot to say - no difference when I added the table name...
 
DOh! I found the problem I think. I had a duplicate column definition in the original program and for some reason it never threw an exception or anything. Once I got rid of the duplicate things started working as expected.

When I used the bound the table it didn't seem to mind but once the bindingsource was added it wouldn't work.
 
Back
Top