how do i do a combobox in the datagridview with a mysql command?

I’m developing an application and I got stuck on the way.
Currently im using this code

adapter.SelectCommand = New MySqlCommand("SELECT id,description from xconfigwarehouses", connection)

table = New DataTable
adapter.Fill(table)
cmb_destino.DataSource = table
cmb_destino.DisplayMember = "description"
cmb_destino.ValueMember = "id"
cmb_destino.SelectedIndex = -1

to open a normal combobox in the application but i wanted to add a column to a datagridview to became something similar
how do i do a combobox in the datagridview with a mysql command?

the project is in visual basic

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Like you have done, except with a datagridviewcombobox instead. Set the DataPropertyName property to the column name of the main table, if you plan on using the combo to update that main table being shown in the grid

Imagine the situation of a Person datatable with a column GenderId that is an int. Also a Gender table with N number of id:name pairs like 1:male, 2:female etc.

personDt = new DataTable
personAdapter.Fill(personDt)

genderDt = new DataTable
genderAdapter.Fill(genderDt)

Then the setup would be:

DataGridView

  • DataSource = personDt

DataGridViewComboBox

  • DataSource = genderDt
  • DisplayMember = “name” ‘e.g. “male”, “female”
  • ValueMember = “id” ‘e.g. 1, 2
  • DataPropertyName = “GenderId” ‘the name of the column in personDt that shall link to

ComboBox becomes 2 way: when it finds 1 in the person gender, it looks it up and shows “male”. When user changes item to “female” it gets associated 2 in genderDt.Id column and stores it to personDt.GenderId


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x