-1

How do we display data from database into DataGridViewComboBoxColumn in Windows form C#?

My database table ticket looks like this: enter image description here

I want to display it into my dataGridView which has two DataGridViewComboBoxColumn named cbxSeverity and cbxStatus:

enter image description here

I tried the code below from the tutorial but it does not work on ComboBoxColumn:

        con.Open();
        cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM Ticket";
        cmd.ExecuteNonQuery();
        dt = new DataTable();
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        dataGridView.DataSource = dt;
        con.Close();

I'm new at this and the tutorial that I watched only used TextBoxColumn, please advise thank you.

lance2k
  • 357
  • 1
  • 4
  • 14
  • There are quite literally hundreds of posts already here on using a `DataGridViewComboBoxColumn`. Another place to start for how to use a `DataGridViewComboBoxColumn` is the documentation – Ňɏssa Pøngjǣrdenlarp May 31 '21 at 19:55
  • For a `DataGridViewComboBoxColumn`, your code must add the column to the grid. It will not be automatically generated. The key is that you need to set the combo box columns `DataPropertyName` to match the name of the column in the table. In this case I assume this may look something like… `comboBoxCol.DataPropertyName = “Severity”;` – JohnG May 31 '21 at 23:36

1 Answers1

0

Thank you for the comments, I didn't add DataPropertyName. Adding it fixed the output

lance2k
  • 357
  • 1
  • 4
  • 14