Accessing MySQL on VB.NET using MySQL Connector/Net, Part 8: Display Result on GUI

This entry is part 8 of 8 in the series Accessing MySQL on VB.NET

Display Result on GUI

Actually, I don’t prepare to write this post while I was writing the series. But there are some people want to know how to display a query data on Windows form rather than in console window which I wrote in the previous post. So this post, I’ll show how to display a query data on DataGridView on Windows form using MySqlDataAdapter.

Step-by-step

  1. I’ll continue from the previous post. You can download a project file from the previous post at here – SampleMySQL (zip format). The project was created on Microsoft Visual Studio 2005.
  2. Open the Design view of Form1.
    Windows Form's Design View
  3. Drag DataGridView tool from the Toolbox window to empty area on the form.
    Note: If you can’t find Toolbox window, select View -> Toolbox.
    Add DataGridView to the form
  4. The DataGridView is placed on the form. The dark background indicates the area of DataGridView’s object. On Properties window, you see the default name is DataGridView1.
    DataGridView1
  5. Back to the Form’s code view. Comment all the lines in Form1_Load method. These are the code from the previous post which I don’t want it to be executed.
    Comment the previous code
  6. Copy the code below to the form as a new method. Notice that this method is similar to retriveData() method except that it use MySqlDataAdapter rather than MySqlDataReader.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    Public Sub retriveDataToDataGrid()
            Try
                Dim query As String = "SELECT * FROM Country"
                Dim connection As New MySqlConnection(connStr)
                Dim da As New MySqlDataAdapter(query, connection)
                Dim ds As New DataSet()
     
                If da.Fill(ds) Then
                    DataGridView1.DataSource = ds.Tables(0)
                End If
     
                connection.Close()
     
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
    End Sub

    Code Explanation:

    • Line 3-5: Create New MySqlDataAdapter object with some parameters.
    • Line 6: Create an empty data set.
    • Line 8-10: Fills a data set and set data source of DataGridView1 to a table in the data set.
    • Line 12: Close the connection.

    retriveDataToDataGrid

  7. Add code to the Form1_Load method to call retriveDataToDataGrid() when the form is loaded.
    Add code on Form_Load method
  8. Run the project. You’ll see the result on DataGridView on Windows form. You may adjust the size of DataGridView to suit your screen.
    The Query Result on DataGridView

Download Code

You can download a complete project file at here – SampleMySQL2. The project was created on Microsoft Visual Studio 2005.

Summary

Now you have reach the end of the article. After 8 parts, you should be able to develop an simple application to access MySQL Server on your own. I think that the article is quite clear than other accessing database server articles that I wrote last year. If you have any question, feel free to leave a comment below.

Reference

Series Navigation<< Accessing MySQL on VB.NET using MySQL Connector/Net, Part 7: Perform SQL Operations

42 Comments

  1. zenyuk July 16, 2009
  2. Md Javed Akhtar October 27, 2009
  3. jaypee November 27, 2009
  4. jaypee November 27, 2009
  5. goodboyanush December 15, 2009
  6. suyan December 31, 2009
  7. Dirk February 2, 2010
  8. linglom February 3, 2010
  9. James February 10, 2010
  10. nono February 11, 2010
  11. linglom February 11, 2010
  12. nono February 11, 2010
  13. nono February 12, 2010
  14. Haxorz February 14, 2010
  15. linglom February 16, 2010
  16. linglom February 16, 2010
  17. nono February 16, 2010
  18. Bernard April 22, 2010
  19. Doug May 17, 2010
  20. Dipta May 19, 2010
  21. linglom May 20, 2010
  22. Dipta May 20, 2010
  23. sabuj and dipta May 21, 2010
  24. linglom fan May 27, 2010
  25. linglom June 7, 2010
  26. jeal4real August 29, 2010
  27. Ismaelaj September 9, 2010
  28. Brian December 30, 2010
  29. napstyr May 2, 2011
  30. Orson May 24, 2011
  31. chieto September 19, 2011
  32. Kaptain Klemmo October 6, 2011
  33. Max Olson October 8, 2011
  34. chrigu May 3, 2012
  35. JackDu May 8, 2012
  36. Sujith July 21, 2012
  37. radha November 4, 2012
  38. rajeve lohani November 17, 2012
  39. Harsh May 27, 2013
  40. ramu June 14, 2013
  41. yonglin January 3, 2014
  42. karl January 21, 2016

Leave a Reply