Listbox filled with system.data.datarowview?????

Hi,

I have an application in which I fill a listbox with values from a dataset.  But the listbox is filled with 'system.data.datarowview' although the dataset contains values because I also fill a datagrid with the same dataset and here the values are displayed correctly.

I use the following code:

   Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dataAccess As New ClassDataAccess
        sql = "select maingroupdescription from tblmaingroup"
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter = dataAccess.Getdataadapter(connstring, sql)
        connection.Open()

        Dim dataset1 As New DataSet
        dataadapter.Fill(dataset1)
        Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView

        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "MainGroupDescription"

    End Sub

Can anybody help me with this hair rising problem?

Thanx,

Tom
zozzieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

testnCommented:
try to swap the order

       Me.ListBox1.DisplayMember = "MainGroupDescription"
       Me.ListBox1.DataSource = dataset1.Tables(0)
0
zozzieAuthor Commented:
I tried your suggestion, but it gives the same result.

Is this a bug in VS.NET?

Thanks,

Tom
0
DranizzCommented:
Just point the datasource of the listbox to the dataview


  Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView
  Me.ListBox1.DataSource = dataset1.Tables(0).DefaultView
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

liebrandCommented:
Specify if the value member as well:

        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "MainGroupDescription"
        Me.ListBox1.ValueMember = "MainGroupDescription"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zozzieAuthor Commented:
Thanks for your replies, but

@ Dranizz : tried it, but it gives the same result

@ Liebrand : tried it and it doesn't work : I get an error message:
 'Additional information: Could not bind to the new display member.'

Any other suggestions??

Tom
0
liebrandCommented:
select an additional column in your query:

sql = "select maingroupdescription as displaymember, maingroupdescription as valuemember from tblmaingroup"

then:

me.listbox1.displaymember = "displaymember"
me.listbox1.valuemember = "valuemember"

NOTE: It is case sensative -- if your displaymember and valuemember do not match your column names in your query it will not work.  In most cases if you are using oracle it will uppercase the column names, and I believe SQL keeps what you entered. The best way to see what is being returned to you is to debug the app, and then output the dataset1.getxml() and match your column name casing that way.
0
DranizzCommented:
How are you using the dataview
where do you specified Sort and Filter properties?
0
zozzieAuthor Commented:
@ Liebrand

Your suggestion works although I left out the .valuemember.

Could you please explain why it has to be coded this way (why do I need an extra statement in the sql-query?

Tom
0
liebrandCommented:
sometimes you might want to display something other than that actual value. For instance, your value might be a customerID...

so you would select customerid as your value member and the customer name as your display member....

your listbox would then SHOW the customer name, but it would use the customer id as the value that gets accessed.

my guess is that it was not the extra statement that did it in the sql, but rather the case of the displaymember value (my guess).
0
zozzieAuthor Commented:
@liebert

I also think it is the displaymember, but now I haveanother problem: suppose I click a MainGroupDescription in the listbox, when I use listbox1.selectedvalue then I suppose I get MainGroupID but this isn't true because when I use the following sql-string for filling up a second listbox:

sql2 = "select groupdescription as displaymember, groupID as valuemember from tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue

it doesn't work because I get an error:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DataRowView' to type 'String' is not valid.


What is the cause of this?

Tom

PS: thanks for your time for helping me
0
liebrandCommented:
post your new code ... i just tested this and it works fine
0
zozzieAuthor Commented:
OK, here's my code


    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim dataAccess As New ClassDataAccess
        sql = "select maingroupdescription as displaymember, maingroupID as valuemember from tblmaingroup"
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter = dataAccess.Getdataadapter(connstring, sql)
        connection.Open()

        dataset1.Clear()
        dataadapter.Fill(dataset1)
        Me.DataGrid1.DataSource = dataset1.Tables(0).DefaultView
        Me.ListBox1.DataSource = dataset1.Tables(0)
        Me.ListBox1.DisplayMember = "displaymember"
        Me.ListBox1.ValueMember = "valuemember"
        connection.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        sql2 = "select groupdescription as displaymember, groupID as valuemember from tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue
        connstring = "Data Source=""C:\softlist\Softlist_database.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"""
        connection = dataAccess.GetConnection(connstring)
        dataadapter2 = dataAccess.Getdataadapter(connstring, sql2)
        connection.Open()
        Me.ListBox2.DataSource = dataset1.Tables(2).DefaultView
        Me.ListBox2.DisplayMember = "displaymember"
        Me.ListBox2.ValueMember = "valuemember"
    End Sub
0
liebrandCommented:
Change your SQL to me.listbox1.selectedValue.toString

NOTE: Since you are databinding, your SelectedIndexChanged event is going to be executed when the databind happens. If this is not your desired result, I suggest you do an AddHandler after the .datasource instead of using the Handles on your ListBox1_SelectedIndexChanged

0
zozzieAuthor Commented:
HI,

I tried your suggestion : "Change your SQL to me.listbox1.selectedValue.toString"

but this doesn't work beacause the method .ToString is not available.  Still I don't understand the problem because the selected value in listbox is an integer just as tblGroup.MainGroupID????

Any ideas?

Thanks  Tom
0
liebrandCommented:
You using Visual Studio .NET 2002 or 2003?
0
DranizzCommented:
The ToString method is available, it just does'nt appear at screen. All objects in énet derived from object class, so th Tostring method is heritate from the object class.
0
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: liebrand {http:#9771344} & Dranizz {http:#9781190}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
pharriesCommented:
Simple answer
I bet that your table contains fields with capitals
Change them all to lower case as VB.net only allows lower case for field names when connecting with databases.
It seems to affect some databases eg (ADS and I gather Oracle) more than others
I spent hours working this out!
so simply rename MainGroupDescription  as maingroupdescription

Hope it helps!
0
adsttuCommented:
I was having this issue an it turned out to be a case-sensitive issue.  The field names in the table, dataset and DisplayMember all have to match case.  Crazy.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.