| | |
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:
Solved Threads: 0
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)
Private Sub cboBarcodeInCust_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBarcodeInCust.SelectedIndexChanged Dim ds As New DataSet Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where AccountNo like = '" & cboBarcodeInCust.SelectedValue & "'", Con) Count.Fill(ds, "BarcodeTotals") dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals") dgWorkwearIn.Columns("AccountNo").Visible = False
•
•
Join Date: Apr 2009
Posts: 16
Reputation:
Solved Threads: 0
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
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)
Public Sub BarcodeTotal() Dim ds As New DataSet Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con) Count.Fill(ds, "BarcodeTotals") dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals") dgWorkwearIn.Columns("AccountNo").Visible = False
•
•
Join Date: Sep 2009
Posts: 321
Reputation:
Solved Threads: 45
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.
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.
•
•
Join Date: Apr 2009
Posts: 16
Reputation:
Solved Threads: 0
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
Probably doing something wrong here to even though the way I have done it still works!
VB.NET Syntax (Toggle Plain Text)
Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con)
•
•
Join Date: Apr 2009
Posts: 16
Reputation:
Solved Threads: 0
0
#6 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?
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!
•
•
Join Date: Sep 2009
Posts: 321
Reputation:
Solved Threads: 45
0
#7 Oct 21st, 2009
I would suggest something like:
VB Syntax (Toggle Plain Text)
Private Sub FillDataset() Using con As New SqlConnection(strMyConnectionString) Dim cmdSelect As New SqlCommand Dim da As New SqlDataAdapter With cmdSelect .Connection = con .CommandType = CommandType.Text .CommandText = "Select * From VwBarcodeTotals Where AccountNo = @AccountNo" .Parameters.AddWithValue("@AccountNo", ComboBox1.SelectedValue.ToString) End With da.SelectCommand = cmdSelect da.Fill(ds, "BarcodeTotals") da.Dispose() cmdSelect.Dispose() End Using 'con End Sub
![]() |
Similar Threads
- Pass selected row in Datagrid view to another datagrid on another form (VB.NET)
- how to print data from datagrid view ? (VB.NET)
- how to add the data into datagrid view (C#)
- How to print from datagrid view ? (VB.NET)
- check box in datagrid view (VB.NET)
- Datagrid view using parameters(Plz help) (VB.NET)
- Using SelectedIndexChanged to change another column on the datagrid (C#)
Other Threads in the VB.NET Forum
- Previous Thread: Switching Between Forms
- Next Thread: Radio buttons and If...Then..If/Else
Views: 473 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date design designer dissertation dissertations dropdownlist excel fade file-dialog ftp generatetags google gridview hardcopy images inline input insert installer intel internet listview mobile monitor ms net networking objects output passingparameters picturebox picturebox1 port print printing problem project remove save searchbox searchvb.net select serial server shutdown soap sorting studio survey syntax table tcp temperature text textbox time timer toolbox trim update updown user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio2008 web winforms wpf





