• We just launched and are currently in beta. Join us as we build and grow the community.

Fill Data Based on DataGridViewComboBoxColumn in C# and MySQL Database

pulledupwithmycock

Developer
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
31
Likes
49
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, I will teach you how to fill the data in the DataGridViewComboBoxColumn using C# and MySQL database. This method has the ability to add a combobox column in the datagridview. It also has the capacity to fill the combobox with data from the database. This method is very useful when you are dealing with datagridview control.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2

Do the form just like shown below.
pscbo_dtg.png

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access MySQL

libraries

  1. using

    MySql.Data.MySqlClient

    ;

Step 4

Establish a connection between C# and MySQL database. After that, declare all the classes and variables that are needed.

  1. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=dbsubjects;sslMode=none"

    )

    ;
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. String

    sql;
  6. DataGridViewComboBoxColumn cbo =

    new

    DataGridViewComboBoxColumn(

    )

    ;

Step 5

Create a method for adding and filling data in the combobox.

  1. private

    void

    fill_combo(

    string

    sql)
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;

  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. cmd.

    Connection

    =

    con;
  10. cmd.

    CommandText

    =

    sql;

  11. da.

    SelectCommand

    =

    cmd;
  12. da.

    Fill

    (

    dt)

    ;

  13. foreach

    (

    DataRow dr in

    dt.

    Rows

    )
  14. {
  15. cbo.

    Items

    .

    Add

    (

    dr.

    Field

    <

    string

    >

    (

    "SUBJ_CODE"

    )

    )

    ;
  16. cbo.

    Name

    =

    "Subject Code"

    ;
  17. }
  18. //add the combobox in the column in the datagridview
  19. dataGridView1.

    Columns

    .

    Add

    (

    cbo)

    ;
  20. }
  21. catch

    (

    Exception ex)
  22. {
  23. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  24. }
  25. finally
  26. {
  27. con.

    Close

    (

    )

    ;
  28. da.

    Dispose

    (

    )

    ;
  29. }

  30. }

Step 6

Writ the following codes to execute the method in the first load of the form.

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. sql =

    "SELECT * FROM subject"

    ;
  4. fill_combo(

    sql)

    ;
  5. }

The complete source code is included you can download it and run it on your computer.

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

335,994

336,002

Top