Solved

Listbox displaying 'System.Data.DataRow'

Posted on 2014-01-26
4
573 Views
Last Modified: 2014-01-27
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

0
Comment
Question by:TimHudspith
  • 3
4 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39810127
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

0
 

Author Comment

by:TimHudspith
ID: 39810246
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.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39810270
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?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39810581
Hi TimHudspith;

I have looked at the code again and did some testing and found out that the ListBox.DataMember is not going to work in this case because the binding to the DataRow collection is to complex. So I came up with an alternative solution. In the code snippet below I made changes to make it work. What you need to do is the following:

In this part of the statement, c.Field(Of String) change String to the data type of the data held by the column you wish to display, if the data in that column is of type String then you do not need to do anything as it is already String. If it is of another data type for example type Integer then change String to Integer and also add .ToString() to the end of this part of the statement, c.Field(Of String)("ColumnName") like so c.Field(Of String)("ColumnName").ToString()

In this part of the statement, ("ColumnName"), change "ColumnName" to the actual column name in the DataRow.

That is all the changes that is needed.

Private Sub lbxOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lbxOne.SelectedIndexChanged
    Dim rows As List(Of String) = Me.lbxOne.DataSource.rows(lbxOne.SelectedIndex).getChildRows("Table1toTable2").Select(Function(c) c.Field(Of String)("ColumnName")).ToList()
    Me.lbxTwo.DataSource = rows
End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now