Load DataView Grid from ComboBox Selection

Neogortex

New member
Joined
Aug 21, 2006
Messages
2
Programming Experience
Beginner
'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.
VB.NET:
DataBase: SQL Server 2005 express
Language: VB.NET 2005
FrameWork: .NET 2.0
 
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data.SqlClient[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] frmCboBoxTest[/SIZE]
 
[SIZE=2][COLOR=#008000]     'Declare Object and Database Connection String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] objConnection [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection _[/SIZE]
[SIZE=2]     ([/SIZE][SIZE=2][COLOR=#800000]"server=OFFSITE-223181S\SQLEXPRESS;database=TemDb;Trusted_Connection=yes;"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
 
[SIZE=2][COLOR=#008000]          'SELECT statement for the ComboBox[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]          Dim[/COLOR][/SIZE][SIZE=2] objDataAdapter2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter( _[/SIZE]
[SIZE=2][COLOR=#800000]          "SELECT Satellite.SatelliteSccNum4 From Satellite"[/COLOR][/SIZE][SIZE=2], objConnection)[/SIZE]
 
 
[SIZE=2][COLOR=#008000]               'SELECT statement for DataGridView And [/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]               'JOIN WHERE Signal Table Transponder ID is Equal to Transponder Table Transponder ID[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]               Dim[/COLOR][/SIZE][SIZE=2] objDataAdapter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter( _[/SIZE]
[SIZE=2][COLOR=#800000]               "SELECT * FROM Signal[/COLOR][/SIZE][SIZE=2], objConnection)[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#008000]          'ComboBox1 connection Information[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]          Dim[/COLOR][/SIZE][SIZE=2] objDataSet2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]          Dim[/COLOR][/SIZE][SIZE=2] objDataView2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView[/SIZE]
 
[SIZE=2][COLOR=#008000]     'DataGrid connection information[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] objDataSet [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] objDataView [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FillDataSetAndView()[/SIZE]
 
[SIZE=2][COLOR=#008000]     'ComboBox1[/COLOR][/SIZE]
[SIZE=2]     objDataSet2 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2]     objDataView2 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataView(objDataSet2.Tables([/SIZE][SIZE=2][COLOR=#800000]"Satellite"[/COLOR][/SIZE][SIZE=2]))[/SIZE]
 
[SIZE=2][COLOR=#008000]          'DataGridView[/COLOR][/SIZE]
[SIZE=2]          objDataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2]          objDataView = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataView(objDataSet.Tables([/SIZE][SIZE=2][COLOR=#800000]"Signal"[/COLOR][/SIZE][SIZE=2]))[/SIZE]
 
 
[SIZE=2][COLOR=#008000]               'Fill the ComboBox with data[/COLOR][/SIZE]
[SIZE=2]               objDataAdapter2.Fill(objDataSet2, [/SIZE][SIZE=2][COLOR=#800000]"Satellite"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#008000]          'Fill the DataGrid with data[/COLOR][/SIZE]
[SIZE=2]          objDataAdapter.Fill(objDataSet, [/SIZE][SIZE=2][COLOR=#800000]"Signal"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BindFields()[/SIZE]
 
[SIZE=2][COLOR=#008000]     'Clear any previous bindings[/COLOR][/SIZE]
[SIZE=2]     ComboBox1.DataBindings.Clear()[/SIZE]
[SIZE=2]     grdCboTest.DataBindings.Clear()[/SIZE]
 
[SIZE=2][COLOR=#008000]          'Set the ComboBox properties to bind it to the data and[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]          'Add Satellite SCC #s' from the Satellite DB[/COLOR][/SIZE]
[SIZE=2]          ComboBox1.DataSource = objDataSet2.Tables([/SIZE][SIZE=2][COLOR=#800000]"Satellite"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]          ComboBox1.DisplayMember = [/SIZE][SIZE=2][COLOR=#800000]"SatelliteSccNum4"[/COLOR][/SIZE]
[SIZE=2]          ComboBox1.ValueMember = [/SIZE][SIZE=2][COLOR=#800000]"SatelliteSccNum4"[/COLOR][/SIZE]
[COLOR=#800000][/COLOR] 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ComboBox1_SelectedIndexChanged( [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ComboBox1.SelectedIndexChanged[/SIZE]
 
[SIZE=2][COLOR=#0000ff]     If[/COLOR][/SIZE][SIZE=2] ComboBox1.SelectedIndex [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][COLOR=#008000]          'Open the database connection[/COLOR][/SIZE]
[SIZE=2]          objConnection.Open() [/SIZE]
 
[SIZE=2][COLOR=#008000]               'Set the DataGridView properties to bind it to the data[/COLOR][/SIZE]
[SIZE=2]               grdCboTest.AutoGenerateColumns = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]               grdCboTest.DataSource = objDataSet[/SIZE]
[SIZE=2]               grdCboTest.DataMember = [/SIZE][SIZE=2][COLOR=#800000]"Signal"[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]                    'Close the Database connection[/COLOR][/SIZE]
[SIZE=2]                    objConnection.Close()[/SIZE]
 
[SIZE=2][COLOR=#008000]               'Fill the dataset and bind the fields[/COLOR][/SIZE]
[SIZE=2]               FillDataSetAndView()[/SIZE]
[SIZE=2]               BindFields()[/SIZE]
 
[SIZE=2][COLOR=#0000ff]     End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmCboBoxTest_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
 
[SIZE=2][COLOR=#008000]     'Fill ComboBox and DataGridView with data from DB at Load[/COLOR][/SIZE]
[SIZE=2]     FillDataSetAndView()[/SIZE]
[SIZE=2]     BindFields()[/SIZE]
 
[SIZE=2][COLOR=#008000]          'Set ComboBox Selected index to the 1st position or 0 index at Load[/COLOR][/SIZE]
[SIZE=2]          ComboBox1.SelectedIndex = 0[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnExit_Click( [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnExit.Click[/SIZE]
 
[SIZE=2][COLOR=#008000]     'Close application when btnExit is Clicked[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     Me[/COLOR][/SIZE][SIZE=2].Close()[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[/SIZE]
 
Last edited:
What you are talking about is a standard Master/Detail relationship. Have a read of this article:

http://msdn2.microsoft.com/en-us/library/c12c1kx4.aspx

the only difference is that for the MASTER you are NOT using a datagridview, but a combobox. It matters little because when you select from the combo it alters the list position of the underlying bindingsource in the same way that clicking on row 2 or row 7 of the master datagridview does.

I suggest you follow the tutorial exactly initially, then try dropping a combo on the form, and binding it to the same source that the master DGV is bound to. If you do not enjoy success, return here and we'll assist you futher
 
This is the fix

Thank you very much, this fixed the problem. I just had to change the DataGridView information to ComboBox.

Thanks, Mike
 
Back
Top