Solved

Listbox displaying 'System.Data.DataRow'

Posted on 2014-01-26
4
566 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

10 Experts available now in Live!

Get 1:1 Help Now