troubleshooting Question

VB.Net: Connect Existing SQL DB to Existing Programmatically Created DataGridView

Avatar of adamchicago
adamchicago asked on
Visual Basic Classic.NET ProgrammingSQL
20 Comments1 Solution527 ViewsLast Modified:
Experts, please help me with code to connect an existing SQL Table to an already EXISTING, programmatically created datagridview.  The major challenge for me is how to write line of code for the datagridview when its name is based on the variable "moduletableCB.text"??

For example if you hardcode a datagridview named "dg" you can write the line like this:
dg.datasource = table    (this would work if it were my situation)

But in this case the datagridviews were programmatically created indexing off of a variable and the user is selecting the datagridview to populate based on the their selection in the combobox "moduletableCB" so I tried this line of code but it doesn't work because my syntax is out of wack:
moduletableCB.datasource = table    (this doesn't work...help!)


Details:
SQL table: name comes from variable "moduletableCB.text"
DataGridView: name comes from variable "moduletableCB.text"

The code in the snippet below is my failed attempt...it creates a NEW datagridview on the form instead of what I need which is connecting an EXISTING datagridview to the SQL Table

Dim dbadp As New SqlDataAdapter("USE " & DBName & ";SELECT * FROM " & "dbo." & moduletableCB.Text, Conn2)
        Dim dTable As New DataTable
        dbadp.Fill(dTable)
        dbadp.Dispose() 
        Dim dgv As New DataGridView
        dgv.Name = Me.moduletableCB.Text
        dgv.Location = New Point(20, 20) 
        Me.Controls.Add(dgv) 
        dgv.DataSource = dTable
        Conn2.Close()
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 20 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros