Selected Value in ComboBox Populates DataGridView

'I need assistance on how to Load the GridView By selecting a value in the ComboBox


'1. ComboBox is loaded with Satellite SCC #s' From SQL Server 2005 Express


'2. The DatGridView is loaded with signals that correspond to the Transponders on the Satellite From SQL Server Express 2005
'The GridView also contains the Satellite SCC #s'.


'3. I need the GridView to only load the Signal that Matches the Satellite SCC #s' that are Selected from the ComboBox.


'4. So when you click the SCC # in the ComboBox, it will search the Signal Database Table for all the matching
'Satellite SCC #s' and then load the signal information into the GridView.

DataBase: SQL Server 2005 express
Language: VB.NET 2005
FrameWork: .NET 2.0

Imports System.Data
Imports System.Data.SqlClient

PublicClass frmCboBoxTest

'Declare Object and Database Connection String
Dim objConnection AsNew SqlConnection _
("server=OFFSITE-223181S\SQLEXPRESS;database=TemDb;Trusted_Connection=yes;")


'SELECT statement for the ComboBox
Dim objDataAdapter2 AsNew SqlDataAdapter( _
"SELECT Satellite.SatelliteSccNum4 From Satellite", objConnection)


'SELECT statement for DataGridView And
'JOIN WHERE Signal Table Transponder ID is Equal to Transponder Table Transponder ID
Dim objDataAdapter AsNew SqlDataAdapter( _
"SELECT * FROM Signal, objConnection)

'ComboBox1 connection Information
Dim objDataSet2 As DataSet
Dim objDataView2 As DataView

'DataGrid connection information
Dim objDataSet As DataSet
Dim objDataView As DataView

PrivateSub FillDataSetAndView()

'ComboBox1
objDataSet2 = New DataSet
objDataView2 = New DataView(objDataSet2.Tables("Satellite"))

'DataGridView
objDataSet = New DataSet
objDataView = New DataView(objDataSet.Tables("Signal"))


'Fill the ComboBox with data
objDataAdapter2.Fill(objDataSet2, "Satellite")

'Fill the DataGrid with data
objDataAdapter.Fill(objDataSet, "Signal")

EndSub

PrivateSub BindFields()

'Clear any previous bindings
ComboBox1.DataBindings.Clear()
grdCboTest.DataBindings.Clear()

'Set the ComboBox properties to bind it to the data and
'Add Satellite SCC #s' from the Satellite DB
ComboBox1.DataSource = objDataSet2.Tables("Satellite")
ComboBox1.DisplayMember = "SatelliteSccNum4"
ComboBox1.ValueMember = "SatelliteSccNum4"

EndSub

PrivateSub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If ComboBox1.SelectedIndex Then

'Open the database connection
objConnection.Open()

'Set the DataGridView properties to bind it to the data
grdCboTest.AutoGenerateColumns = True
grdCboTest.DataSource = objDataSet
grdCboTest.DataMember = "Signal"

'Close the Database connection
objConnection.Close()

'Fill the dataset and bind the fields
FillDataSetAndView()
BindFields()

EndIf

EndSub

PrivateSub frmCboBoxTest_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load

'Fill ComboBox and DataGridView with data from DB at Load
FillDataSetAndView()
BindFields()

'Set ComboBox Selected index to the 1st position or 0 index at Load
ComboBox1.SelectedIndex = 0

EndSub

PrivateSub btnExit_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

'Close application when btnExit is Clicked
Me.Close()

EndSub

EndClass



Answer this question

Selected Value in ComboBox Populates DataGridView

  • Tryin2Bgood

    I commented them out that did not make a change

    I can't send the project because of company policies - I wish I could, I have been working this issue for about 4.5 days

    You could make an easy project - I just used GridView, ComboBox and SQL Server 2005 Express


  • Marc Lacoursiere

    I can't use Me.grdCboTest.SelectedItems.ToString()) VS 2005 will only let use Me.grdCboTest.SelectedCells.ToString())

    and if I use SelectedCells it errors out


  • twolfkg

    I'm a retard; I chose the GridView instead of the ComboBox

    Now I'm getting the error: Cannot find column [System.Data.DataRowView].


  • Peter Rohlfs

    it should be an array as I had shown in my snippet, you have only declared it as a single object (non array) :-)

    dim theResults() as DataRow



  • Muricy

    I changed Me.grdCboTest.SelectedItems.ToString()) to Me.grdCboTest.SelectedValue.ToString()) and now when I select the ComboBox I am getting the Table:

    RowState | Table | HasError | RowErrors|

    Unchanged | Blank | Blank | Blank |


  • Meltdown61

    Signal is a Table

    Here is all of the code

    Imports System

    Imports System.Data

    Imports System.Data.SqlClient

    Imports System.Windows.Forms

    Public Class frmCboBoxTest

    Inherits System.Windows.Forms.Form

    'Declare Object and Database Connection String

    Dim objConnection As New SqlConnection _

    ("server=OFFSITE-223181S\SQLEXPRESS;database=TemDb;Trusted_Connection=yes;")

    'SELECT statement for the ComboBox

    Dim objDataAdapter2 As New SqlDataAdapter( _

    "SELECT Satellite.SatelliteSccNum4 From Satellite", objConnection)

    'SELECT statement for DataGridView And

    'JOIN WHERE Signal Table Transponder ID is Equal to Transponder Table Transponder ID

    Dim objDataAdapter As New SqlDataAdapter( _

    "SELECT * FROM Signal " & _

    "JOIN Transponder ON Signal.TransID2 = Transponder.TransponderID " & _

    "JOIN Satellite ON Transponder.SatSccNum = Satellite.SatelliteSccNum4 " & _

    "WHERE Transponder.SatSccNum = Satellite.SatelliteSccNum4", objConnection)

    'ComboBox1 connection Information

    Dim objDataSet2 As DataSet

    Dim objDataView2 As DataView

    'DataGrid connection information

    Dim objDataSet As DataSet

    Dim objDataView As DataView

    Private Sub FillDataSetAndView()

    'ComboBox1

    objDataSet2 = New DataSet

    objDataView2 = New DataView(objDataSet2.Tables("Satellite"))

    'DataGridView

    objDataSet = New DataSet

    objDataView = New DataView(objDataSet.Tables("Signal"))

    'objDataView.RowFilter = "SatelliteSccNum4=-9999999"

    'Fill the ComboBox with data

    objDataAdapter2.Fill(objDataSet2, "Satellite")

    'Fill the DataGrid with data

    objDataAdapter.Fill(objDataSet, "Signal")

    End Sub

    Private Sub BindFields()

    'Clear any previous bindings

    ComboBox1.DataBindings.Clear()

    grdCboTest.DataBindings.Clear()

    'Set the ComboBox properties to bind it to the data and

    'Add Satellite SCC #s' from the Satellite DB

    ComboBox1.DataSource = objDataSet2.Tables("Satellite")

    ComboBox1.DisplayMember = "SatelliteSccNum4"

    ComboBox1.ValueMember = "SatelliteSccNum4"

    'Assign the dataview to the datagrid

    'grdCboTest.AutoGenerateColumns = True

    'grdCboTest.DataSource = objDataView

    'grdCboTest.DataMember = "Signal"

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    If ComboBox1.SelectedIndex Then

    'Set the dataview's row filter to the currently selected value

    'objDataView.RowFilter = "SatelliteSccNum4=" & ComboBox1.SelectedValue

    'Open the database connection

    objConnection.Open()

    'Dim theResults() as DataRow

    'theResults = theDataSet.Tables(index).Select("Field = '" + theComboBox.SelectedValue.ToString() + "'")

    Dim theQuery() As DataRow

    theQuery = objDataSet.Tables("Signal").Select("SatelliteSccNumber = '" + ComboBox1.SelectedValue.ToString + "'")

    'Set the DataGridView properties to bind it to the data

    grdCboTest.AutoGenerateColumns = True

    grdCboTest.DataSource = theQuery 'objDataView 'objDataSet

    grdCboTest.DataMember = "Signal"

    'Close the Database connection

    objConnection.Close()

    'Fill the dataset and bind the fields

    FillDataSetAndView()

    BindFields()

    End If

    End Sub

    Private Sub frmCboBoxTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Fill ComboBox and DataGridView with data from DB at Load

    FillDataSetAndView()

    BindFields()

    'Set ComboBox Selected index to the 1st position or 0 index at Load

    'ComboBox1.SelectedIndex = 0

    End Sub

    Private Sub btnExit_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

    'Close application when btnExit is Clicked

    Me.Close()

    End Sub

    End Class


  • FergusLogic

    Getting Error Below:

    Thank you for your help, I have been workingon this for a while now, Any help is appreciated

    Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    If ComboBox1.SelectedIndex Then

    'Set the dataview's row filter to the currently selected value

    objDataView.RowFilter = "SatelliteSccNum4=" & ComboBox1.SelectedValue

    'Open the database connection

    objConnection.Open()

    'Dim theResults() as DataRow

    'theResults = theDataSet.Tables(index).Select("Field = '" + theComboBox.SelectedValue.ToString() + "'")

    Dim theQuery() As DataRow

    theQuery = objDataSet.Tables("Signal").Select("SatelliteSccNumber = '" + ComboBox1.SelectedValue.ToString() + "'")

    'Set the DataGridView properties to bind it to the data

    grdCboTest.AutoGenerateColumns = True

    grdCboTest.DataSource = theQuery 'objDataView 'objDataSet

    grdCboTest.DataMember = "Signal" Error: "Child list for field Signal cannot be created." When I run the app.

    'Close the Database connection

    objConnection.Close()

    'Fill the dataset and bind the fields

    FillDataSetAndView()

    BindFields()

    End If

    End Sub


  • 2162

    comment out the "FillDataSetAndView() BindFields()" - I think this could be overriding what you have set in the controls.

    you may wish to email me the database along with your project and see if I can see where you are going wrong also and post back the solution. Email address in profile.



  • Matt Lin

    The code below will load the entire Signal table on ComboBox1.SelectedIndex

    If I remove Signal and place TransID2 in place of it, nothing will load but if I place Signal back in the Signal Table will load

    If I comment out grdCboTest.DataMember = "Signal" with your code I get something in the GridView but it is not data, I get clomn names that are error and other name than what is in the Signal Table

    I also tried placeing the string of code in front of grdCboTest.DataSource = objDataSet and I received the same thing in the Grid table as when I used the code you gave me.

    It is getting nearer - Again I thank you greatly for the help - I have this on 3 other VB.NET Forums and know one else wants to try it.

    Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    If ComboBox1.SelectedIndex Then

    'Set the dataview's row filter to the currently selected value

    'objDataView.RowFilter = "SatelliteSccNum4=" & ComboBox1.SelectedValue

    'Open the database connection

    objConnection.Open()

    'Dim theResults() as DataRow

    'theResults = theDataSet.Tables(index).Select("Field = '" + theComboBox.SelectedValue.ToString() + "'")

    'Dim theQuery() As DataRow

    'theQuery = objDataSet.Tables("Signal").Select("SatelliteSccNumber = '" + ComboBox1.SelectedValue.ToString + "'")

    'Set the DataGridView properties to bind it to the data

    grdCboTest.AutoGenerateColumns = True

    grdCboTest.DataSource = objDataSet '.Tables("Signal").Select("SatelliteSccNumber = '" + ComboBox1.SelectedValue.ToString + "'") 'theQuery 'objDataView

    grdCboTest.DataMember = "Signal"

    'Close the Database connection

    objConnection.Close()

    'Fill the dataset and bind the fields

    FillDataSetAndView()

    BindFields()

    End If

    End Sub


  • Flame Thrower

    sure.

    Well, this is the code I used:



    Me.theDataGridView.DataSource = Me.theDataSet.Tables(0).DefaultView;
    ..

    'SelectedIndexChanged event of the combobox:
    Dim theResults() as DataRow
    theResults = Me.theDataSet.Tables(0).Select("[ID] = " Me.theComboBox.SelectedItem.ToString())

    Me.theDataGridView.DataSource = theResults


    this is say a table with:

    ID

    Name

    Address

    and when the combobox item is chosen, the query runs/executes and binds the results to the datagridview



  • srathee

    I am getting a message: Value of type '1 dimensional array of System.Data.DataRow' cannot be converted to 'System.Data.DataRow'.

    Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    If ComboBox1.SelectedIndex Then

    'Open the database connection

    objConnection.Open()

    'Dim theResults() as DataRow

    'theResults = theDataSet.Tables(index).Select("Field = '" + theComboBox.SelectedValue.ToString() + "'")

    Dim theQuery As DataRow

    theQuery = objDataSet.Tables("Signal").Select("SatelliteSccNumber = '" + ComboBox1.SelectedValue.ToString() + "'") 'Get a line through this satatement and the ' 'message above

    'Set the DataGridView properties to bind it to the data

    grdCboTest.AutoGenerateColumns = True

    grdCboTest.DataSource = theQuery

    grdCboTest.DataMember = "Signal"

    'Close the Database connection

    objConnection.Close()

    'Fill the dataset and bind the fields

    FillDataSetAndView()

    BindFields()

    End If

    End Sub


  • Kristian Sogaard

    well when you set the Datamember of the combobox to signal, that would be incorrect as there is no field in the data/table called signal.

    you would have to make sure that you set the datamember to the field that does exist in the table, such as ID for example



  • Uranya

    is "Signal" a field or the name of the table

  • Aseem Chiplonkar

    most likely you will need to filter your records either in SQL or in the dataset. In SQL you will need a WHERE clause. This will be executed when you say, select an item from the combobbox, on the selectedindexchanged event (double click the combobox to create the event)

     

    the other way would be to get hold of the dataset object then query it:

     

    Dim theResults() as DataRow

    theResults = theDataSet.Tables(index).Select("Field = '" + theComboBox.SelectedValue.ToString() + "'")

     

    this will return back all your results in the variable "theResults", then simply set the datagridview datasource to theResults variable.

     

    does this help



  • Selected Value in ComboBox Populates DataGridView