Having a problem with datagrid view and combobox

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 16
Reputation: scooby36 is an unknown quantity at this point 
Solved Threads: 0
scooby36 scooby36 is offline Offline
Newbie Poster

Having a problem with datagrid view and combobox

 
0
  #1
Oct 20th, 2009
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





  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: scooby36 is an unknown quantity at this point 
Solved Threads: 0
scooby36 scooby36 is offline Offline
Newbie Poster
 
0
  #2
Oct 20th, 2009
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
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
1
  #3
Oct 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: scooby36 is an unknown quantity at this point 
Solved Threads: 0
scooby36 scooby36 is offline Offline
Newbie Poster
 
0
  #4
Oct 21st, 2009
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
  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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #5
Oct 21st, 2009
A primary key sounds perfect for filtering your database. Let me ask to be sure, is AccountNo a numeric datatype or text/string datatype?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: scooby36 is an unknown quantity at this point 
Solved Threads: 0
scooby36 scooby36 is offline Offline
Newbie Poster
 
0
  #6
Oct 21st, 2009
Originally Posted by TomW View Post
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #7
Oct 21st, 2009
I would suggest something like:

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: scooby36 is an unknown quantity at this point 
Solved Threads: 0
scooby36 scooby36 is offline Offline
Newbie Poster
 
0
  #8
Oct 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #9
Oct 24th, 2009
Glad it works for ya. Dont forget to update the thread as solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 473 | Replies: 8
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC