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

How to Fill ComboBox with Data inside the DataGridView in C# and MS Access Database

napster009

Community Engagement Wizard
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
143
Likes
141
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This time, I will teach you how to fill combobox with data in the datagridview in C# and MS Access Database. This is very useful most especially when you are dealing with combo box and datagridview control. In this way, you can manipulate the data inside a combobox easily.
Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in C#.
creating_application_c_5.png

Step 2

Do the form just like shown below.
2019-04-29_6.png

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB

to access OLeDB

libraries.
  1. using

    System.Data.OleDb

    ;

Step 4

Create a connection between the access database and c#. After that, declare all the classes that are needed.
  1. OleDbConnection con =

    new

    OleDbConnection(

    "Provider = Microsoft.ACE.OLEDB.12.0; Data Source ="

    +

    Application.

    StartupPath

    +

    "/studentdb.accdb"

    )

    ;
  2. OleDbCommand cmd;
  3. OleDbDataAdapter da;
  4. DataTable dt;
  5. string

    sql;

Step 5

Create a method for adding the comboxbox column with data in the datagridview.

  1. private

    void

    fill_combo(

    string

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

    Open

    (

    )

    ;

  6. cmd =

    new

    OleDbCommand(

    )

    ;
  7. da =

    new

    OleDbDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;
  9. DataGridViewComboBoxColumn cbo =

    new

    DataGridViewComboBoxColumn(

    )

    ;

  10. cmd.

    Connection

    =

    con;
  11. cmd.

    CommandText

    =

    sql;
  12. da.

    SelectCommand

    =

    cmd;
  13. da.

    Fill

    (

    dt)

    ;

  14. cbo.

    HeaderText

    =

    "Name"

    ;
  15. cbo.

    DataSource

    =

    dt;
  16. cbo.

    ValueMember

    =

    "ID"

    ;
  17. cbo.

    DisplayMember

    =

    "fname"

    ;

  18. dataGridView1.

    Columns

    .

    Add

    (

    cbo)

    ;

  19. }
  20. catch

    (

    Exception ex)
  21. {
  22. MessageBox.

    Show

    (

    ex.

    Message

    )

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

    Close

    (

    )

    ;
  27. da.

    Dispose

    (

    )

    ;
  28. }
  29. }

Step 6

Do the following codes to execute the method that you have created in the first load of the form.

  1. private

    void

    Form1_Load(

    object

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

    "Select * From tblstudent"

    ;
  4. fill_combo(

    sql)

    ;
  5. }

Download the complete sourcecode 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,292

324,736

324,744

Top