944,192 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1004
  • VB.NET RSS
Oct 20th, 2009
0

Having a problem with datagrid view and combobox

Expand Post »
I cannot seem to get the datagrid to update with the correct data from the combobox selection the program just crashes. Do I need to bind to the combobox some how?? Any help or advise would be apreciated





VB.NET Syntax (Toggle Plain Text)
  1. Private Sub cboBarcodeInCust_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBarcodeInCust.SelectedIndexChanged
  2. Dim ds As New DataSet
  3. Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where AccountNo like = '" & cboBarcodeInCust.SelectedValue & "'", Con)
  4. Count.Fill(ds, "BarcodeTotals")
  5.  
  6. dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals")
  7. dgWorkwearIn.Columns("AccountNo").Visible = False
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scooby36 is offline Offline
21 posts
since Apr 2009
Oct 20th, 2009
0
Re: Having a problem with datagrid view and combobox
Hi folks,
solved it!!! Had the "=" sign in the select statement and I put the code into its own sub and had the selectedindexchanged event call it works fine now
here is code if helps anyone else
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub BarcodeTotal()
  2. Dim ds As New DataSet
  3. Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con)
  4. Count.Fill(ds, "BarcodeTotals")
  5.  
  6. dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals")
  7. dgWorkwearIn.Columns("AccountNo").Visible = False
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scooby36 is offline Offline
21 posts
since Apr 2009
Oct 20th, 2009
1
Re: Having a problem with datagrid view and combobox
The equals sign is not the problem, its the method of retrieving your text from the combobox that changed. A combox item can have two values, the DisplayMember which is the text that is displayed as each item in the cbo and a hidden value can be assigned to each item in the ValueMember property. cbo.SelectedValue will return the value member but you also need to convert its value into its proper datatype. Such as

cbo.SelectedValue.ToString
or
CInt(cbo.SelectedValue)

Two things I would suggest, for getting the display text as it appears you are trying to do, use the following
cboBarcodeInCust.GetItemText(cboBarcodeInCust.SelectedItem)

The SelectedIndexChanged event will fire multiple times when your first loading your combobox. I would suggest either adding coding to exit the event during your initial load/fill or move your coding to the SelectionChangeCommitted event.
Last edited by TomW; Oct 20th, 2009 at 8:49 pm.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Oct 21st, 2009
0
Re: Having a problem with datagrid view and combobox
Thanks for your help. I changed the code into theSelectionChangeCommitted event and it works a treat now. The Value Member of the combo box is accountNo which is the primary key from the sql database but I could only get the datagrid to filter when I added the Account name ,which is the combbox display member, to the sql select statement
VB.NET Syntax (Toggle Plain Text)
  1. Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con)
Probably doing something wrong here to even though the way I have done it still works!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scooby36 is offline Offline
21 posts
since Apr 2009
Oct 21st, 2009
0
Re: Having a problem with datagrid view and combobox
A primary key sounds perfect for filtering your database. Let me ask to be sure, is AccountNo a numeric datatype or text/string datatype?
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Oct 21st, 2009
0
Re: Having a problem with datagrid view and combobox
Click to Expand / Collapse  Quote originally posted by TomW ...
A primary key sounds perfect for filtering your database. Let me ask to be sure, is AccountNo a numeric datatype or text/string datatype?

Good question. The primary key is string. It is from database that I didn't design I would of used Numeric ident as primary but never mind!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scooby36 is offline Offline
21 posts
since Apr 2009
Oct 21st, 2009
0
Re: Having a problem with datagrid view and combobox
I would suggest something like:

VB Syntax (Toggle Plain Text)
  1. Private Sub FillDataset()
  2.  
  3. Using con As New SqlConnection(strMyConnectionString)
  4. Dim cmdSelect As New SqlCommand
  5. Dim da As New SqlDataAdapter
  6.  
  7. With cmdSelect
  8. .Connection = con
  9. .CommandType = CommandType.Text
  10. .CommandText = "Select * From VwBarcodeTotals Where AccountNo = @AccountNo"
  11. .Parameters.AddWithValue("@AccountNo", ComboBox1.SelectedValue.ToString)
  12. End With
  13.  
  14. da.SelectCommand = cmdSelect
  15. da.Fill(ds, "BarcodeTotals")
  16.  
  17. da.Dispose()
  18. cmdSelect.Dispose()
  19. End Using 'con
  20.  
  21. End Sub
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Oct 23rd, 2009
0
Re: Having a problem with datagrid view and combobox
Thats great TomW! Had a few other select statements like that and have changed them too. probably alot safer as well to use Parameters. Thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scooby36 is offline Offline
21 posts
since Apr 2009
Oct 24th, 2009
0
Re: Having a problem with datagrid view and combobox
Glad it works for ya. Dont forget to update the thread as solved.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Switching Between Forms
Next Thread in VB.NET Forum Timeline: Radio buttons and If...Then..If/Else





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC