0

I'm asking once more, since I'm really new in vb, I am using visual studio 2010 and mysql for my database, I need help getting different columns from different tables but same database and load it into one datagridview. Any kind of help or tips would be much appreciated . Please and Thank you.

1
  • Do you know how to join tables in sql? Commented Oct 22, 2012 at 8:37

1 Answer 1

1

One way would be to use a DataTable with all joined columns as datasource which you can fill with a DataAdapter:

Private Function GetDataSource() As DataTable
    Const sqlSelect As String = "SELECT a.Col1 AS aCol1,a.Col2 AS aCol2,b.Col1 AS bCol1,b.Col2 AS bCol2 " & _
                               "FROM dbo.TableA AS a INNER JOIN dbo.TableB AS b ON a.IdCol=b.aIdCol " & _
                               "ORDER BY aCol1 ASC,bCol1 ASC"
    Try
        Dim table = New DataTable()
        Using con = New MySqlConnection(My.Settings.MySqlConnectionString)
            con.Open()
            Using da = New MySqlDataAdapter(sqlSelect, con)
                da.Fill(table)
                Return table
            End Using
        End Using
    Catch ex As Exception
        ' log message instead '
        Throw ' don't use throw new Exception or throw ex '
    End Try
End Function

Now you can use this DataTable as DataSource for the DataGridView:

me.dataGridView1.DataSource = GetDataSource()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for replying... I'm going to try using this now... I'll update you. Thanks again.
@TimSchemlter Please do bear with me I am just really new with vb and sql... what is this line for "ON a.IdCol=b.aIdCol"?
@user1746113: Sorry, i've overlooked the MySQL tag in the first place, edited the answer accordingly to take that into account. An inner join will only select records where the joined keys are in both specified tables. Since you haven't shown your data model, i have used exemplary columns. In this case TableA is the parent table and TableB the child table which references TableA in a foreign key column. So TableB.aIdCol is just an example for a column which references the primary key of TableA. The ON clause joines both table.
@TimSchemlter ohh thank you and sorry I forgot to place my data model as well.. I haven't got the hang of posting here. hmmm, I have 2 tables in my database forms, 1st table is formcreation and 2nd table is formtable. form creation has 4 columns ID(pk, AI), FormCode(double), FormName(varchar), HasSeries(text). and for formtable I (originally) had 4 columns aswell ID(PK, AI) I removed this because I thought there might be link problems (again Im sorry I don't know the exact term for that as I am really new with all this), Description(varchar), SeriesFrom(double), and SeriesTo(Double).
@TimSchemlter Thank you for your answer with a little tweeks I managed to use it... :D I also found another way of doing it via drag drop :D social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/… Smoore16's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.