Link to home
Start Free TrialLog in
Avatar of TimHudspith
TimHudspith

asked on

Listbox displaying 'System.Data.DataRow'

I have two listboxes on a form representing two tables from a SQL Server database in a parent-child relationship.

The second listbox populates on clicking the first listbox, but although listbox2 shows the correct number of rows, the correct values are not displayed, just 'System.Data.DataRow'.

 Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
        Dim rows() As DataRow = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2")
        Me.lbxTwo.DataSource = rows
    End Sub

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi TimHudspith;

Although you have connected the records to be associated with the second ListBox you have not told it which column to to display in it. Please see the code snippet and change the string ColumnName to a string name of the actual column name to be displayed.

Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
    Dim rows() As DataRow = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2")
    Me.lbxTwo.DataSource = rows
    Me.lbxTwo.DisplayMember = "ColumnName"    
End Sub

Open in new window

Avatar of TimHudspith
TimHudspith

ASKER

I've done that but it makes no difference. Strangely, I can actually set DisplayMember to a column name that even doesn't exist and there's no exception thrown.
Hi TimHudspith;

Setting the DisplayMember property incorrectly will NOT throw an exception but will use the objects ToString method to supply the string and the reason why you get  "System.Data.DataRow' as the object being displayed.

When you typed the name as a string did you use the same case for all the characters?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial