Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old June 4th, 2004, 11:02 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

That's pretty much the way I'm doing it EXCEPT that I'm passing DataRows from a dataset table to the Add instead of MyListBoxItems. The table is being created manually, and I am adding an additional record first (SELECT ALL) that seems to cause problems for the combobox which insists the SelectedValue for the added record is DBNULL rather than zero as set by my code. But I've wrapped all that in a Try/Catch as well.

Thanks for the offer but I've troubled you enough. It works, although it feels like a kluge.
Reply With Quote
  #12 (permalink)  
Old June 4th, 2004, 11:17 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

How do you add the first item? Just using Items.Add? Or do you manually create a DataRow as well?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #13 (permalink)  
Old June 4th, 2004, 12:16 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

When I started with this, I didn't know how to use anything but a table bound to the combobox. So I created a working dataset and added a new row to it containing the values "SELECT ALL" (DisplayMember) and 0 (ValueMember):

with dsWorking.tables(0)
  drEmployee = .NewRow()
  drEmployee("EEID") = 0
  drEmployee("EmployeeName") = " SELECT ALL"
  .Rows.Add(drEmployee)
end with

I then added the records I wanted from another dataset to my working dataset (to eliminate duplicates):

For intRec = 0 To with dsEmployees.tables(0).Rows.Count - 1
   With dsWorking.Tables(0)
      drEmployee = dsEmployees.Tables(0).NewRow()

      intEEID = dsEmployees.Tables(0).Rows(intRec)("EEID")

      drEmployee("EEID") = intEEID
      drEmployee("EmployeeName") = dsEmployees.Tables(0).Rows(intRec)("EmployeeName")

      Try ' used to prevent duplicates being added
          .Rows.Add(drEmployee)
      Catch
      End Try
  End With
Next

I successfully bound the working dataset to the combobox, but the records weren't sorted the way I wanted. That was when I discovered I couldn't set the Sorted property on the combobox if it was bound. So I removed the binding and manually added each of the datarows to the combobox using this code:

With dsWorking.Tables(0)
  For intPos = 0 To .Rows.Count - 1
    With cboName
      .Items.Add(dsWoprking.Tables(0).Rows(intPos))
    End With
  Next
End With

My code is actually more complex than what I've shown above because I'm performing recursive queries against a view to walk the chain of command, but you should get the drift from what I've shown above (and hopefully I've not introduced any syntax or spelling errors in my translation). This got me what I wanted, although I had to do a lot of dancing to get there, hence my comments about simplicity earlier. And then, of course, I discovered the problem where the manually added record added at the top of all this was returning DBNULL for the ValueMember value. And here we are a day later...

------------------------------------------------------------------

In a related vein, I've just noticed something bizarre with some other comboboxes I'm using. I've created three of them, and bound them all to the same table because the list of values in each is the same, but the user may choose a different value in each. Setting SelectedIndex for any one of these comboboxes causes ALL of them to be set. It looks as though SelectedIndex is therefore associated with the TABLE being bound and NOT the combobox. I assume the same will be true of SelectedItem and SelectedValue then, too, although I haven't wasted my time trying to prove that. So, in order to have three independent comboboxes with the same set of data to choose from I need three tables, even though they all contain the same data? What kind of lunacy is that? That's a rhetorical question, of course. Guess I'll be removing the binding on these comboboxes as well and adding the values manually as above. I wonder what new weirdness I'll stumble across when I try that. <sigh>
Reply With Quote
  #14 (permalink)  
Old June 5th, 2004, 05:43 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, like you said: at least it's working now.
What you could try is this:

1. Create the DataSet
2. Create a DataView from the DefaultView of your DataTable and sort it. Then bind it to the drop down instead of binding the table directly:
Code:
  sqlDataAdapter1.Fill(dataSet1);
  DataView myView = dataSet1.Tables[0].DefaultView;
  myView.Sort = "Description";

  comboBox1.DataSource = myView;
  comboBox1.DisplayMember = "Description";
  comboBox1.ValueMember = "ID";
  (This assumes you have a SELECT statement like SELECT ID, Description FROM SomeTable)

Quite possible, this will get you into another Catch-22: How to add the SELECT ALL item.... ;)

I don't know what to say about the other problem. I tried it, and I got the exact same behavior. Where do I send that money for the hit fund??

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #15 (permalink)  
Old June 7th, 2004, 07:08 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Actually, since I add the " SELECT ALL" option to the dataset prior to the sort, and the leading space tricks the sort into putting it first in the list, that shouldn't be a problem. However, as I'm not sure whether this would resolve the DBNULL issue related to that record, and would require changing code and retesting something that does work, albeit not as hoped, I'm reluctant to make changes to this particular code. I expect more of this sort of thing to occur in the future, though, so when the next one comes up I'll give this a try in lieu of what I've worked out.

Thanks again for your help undesrstanding this.
Reply With Quote
  #16 (permalink)  
Old June 10th, 2004, 03:03 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Here's a follow-up for any inquiring minds out there. I have come to the conclusion that BOUND comboboxes simply do not work as advertised. I hope somebody from MS is following this:

In brief, my windows form was supposed to be a simple data entry form that would load an existing record, allow the user to change values, and then save it. When the form was first displayed, all the fields were supposed to be blank except for the comboboxes, which contained the list of possible values, of course, and displayed the text "Select or Enter New Value". IOW, this is a true combobox where a new value could be entered at any time.

Then when the user selected a value from one combobox, the program read a corresponding record from a SQL table and populated all the fields on the form with values from the record, which the user could then change and save. The weirdness came when I tried to display records where the value to be selected from the bound combobox was the FIRST value in the list. If I selected said record when the fields on the entry form were blank, it would NOT select the value no matter what I tried. .SelectValue would return NOTHING after I set it. Iterating through the values and setting the .SelectedIndex would return the correct index value, but it still would not display the first item in the list (instead it persisted in displaying the "Select or Enter New Value" message which I set the .Text property to). And .SelectedItem, as I've previously mentioned, has never worked at all.

Then it got really weird. I discovered that if I selected another record first, where the value to be displayed in the combobox was NOT the first value in the list, I was able to select it properly. And then if I changed to the record containing the value that was first in the list, it would select the value for the record. So, basically the first time in it would not work, but if I selected another record first, then it would select the expected value from the record that failed previously.

I cannot explain why it would not work the first time when the value to be selected was the first one in the list - it ran through the very same code each time. Perhaps this is somehow related to the earlier problem I described where the bound combobox was returning DBNULL for my manually added row even though I could prove I'd written a 0 to it. I did not do it that way this time - the "Select..." option was simply put into the .Text property and not added to the table that was being bound. Whatever the reason, though, it would not work.

After banging my head on this for an hour, I abandoned this approach and instead took to using the combobox.items.add(string) Imar and I discussed. I first added the "Select..." option to the combobox and then each of the table records manually. And suddenly this problem went away! There should be no fundamental difference between the two processes, but the bound combobox refused to cooperate.

I'm not expecting anybody to help me fix this, or even understand it. I've found a work around that works and don't really care why it doesn't any more - I have otehr problems to get onto and have wasted enough time fighting with this one. This is simply provided as a public service announcement to anybody else having problems with bound comboboxes:

GIVE UP! Do not bind your comboboxes - that's more headache than it's worth. Write the extra code to populate them manually and be prepared to iterate through the values to set the selected one. I will never bind a combobox again. You will save yourself a lot of trouble and time.
Reply With Quote
  #17 (permalink)  
Old January 26th, 2005, 04:15 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

 
Quote:
quote:I've just noticed something bizarre with some other comboboxes I'm using. I've created three of them, and bound them all to the same table because the list of values in each is the same, but the user may choose a different value in each. Setting SelectedIndex for any one of these comboboxes causes ALL of them to be set. It looks as though SelectedIndex is therefore associated with the TABLE being bound and NOT the combobox. I assume the same will be true of SelectedItem and SelectedValue then, too, although I haven't wasted my time trying to prove that. So, in order to have three independent comboboxes with the same set of data to choose from I need three tables, even though they all contain the same data? What kind of lunacy is that?
Hi Ron,

It's been a while since we discussed the odd behavior of Windows combo boxes in .NET.

A while ago, I read an interesting article about this behavior in some magazine and I immediately had to think about this thread. After some Googling I found an on-line version of the article so I'd thought I'd post it here as a follow up.

You can find the article here: http://www.code-magazine.com/article...printmode=true

It's quite an interesting read if you want to understand how databinding in Win Forms works....

You aren't "the reader" that Dino talks about, are you? ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Funky Dreamer by MC Solaar (Track 16 from the album: Qui Sème le Vent Récolte le Tempo) What's This?
Reply With Quote
  #18 (permalink)  
Old April 11th, 2005, 07:53 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

I am not familiar with "Dino", but his srticle describes exactly the situation I was experiencing in my sidebar in this discussion. As I said elsewhere, I've taken to manual population of most controls these days to avoid these problems. This makes for more work, especially when dealing with datagrids, but appears to remove all uncertainty where it comes to binding related issues.
Reply With Quote
  #19 (permalink)  
Old June 28th, 2007, 06:01 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This topic is really old, but in case others stumble across this: If you want to manually add records to a combobox with the value included, simply use an arraylist of class properties. This is similar to the above with slight modifications.

The valuemember is meant to reference a class property that is specified in the datasource. This comes straight from http://msdn2.microsoft.com/en-us/lib...luemember.aspx

This way you can use SelectedValue as intended. Hope this helps.

Code:
Friend Class vpair
    Public myText As String
    Public myValue As Integer

    Public Sub New(ByVal strText As String, ByVal intValue As Integer)
        Me.myText = strText
        Me.myValue = intValue
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return myText
        End Get
    End Property

    Public ReadOnly Property Value() As String
        Get
            Return myValue
        End Get
    End Property


    Public Overrides Function toString() As String
        Return Me.Value & " - " & Me.Text
    End Function

End Class
Code:
            Dim priorities As New ArrayList()
            priorities.Add(New vpair("Low Priority", 1))
            priorities.Add(New vpair("Med Priority", 2))
            priorities.Add(New vpair("High Priority", 3))

            cboPriority.Items.Clear()

            cboPriority.DataSource = priorities
            cboPriority.DisplayMember = "Text"
            cboPriority.ValueMember = "Value"
Reply With Quote
  #20 (permalink)  
Old February 13th, 2008, 02:54 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Bill: hope you see this. I can't reply to your e-mail, but you're not really asking a question here either.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox Populating Delay aw23 Access VBA 24 June 8th, 2005 03:39 AM
populate data in a windows form combobox from a da anibiswas General .NET 0 March 4th, 2005 03:57 PM
Populating report or form textboxes Bosstone100 Access VBA 3 November 15th, 2004 03:53 PM
Populating a form maneor Access 1 September 30th, 2004 03:41 PM
Populating Combobox by Code Syed Afzal Ahmed ADO.NET 1 July 12th, 2003 03:34 AM



All times are GMT -4. The time now is 06:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.