0

I pull the data from sql database and fill datagridView1 with it:

dataGridView1.DataSource = bsour;

User will be able to edit value in 1 column, but possible values for that column are only Null, 0, 1 or 2 so to prevent from inputting incorrect value i decided to do a ComboBoxColumn:

DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Name = "il_normal_combo";
col1.HeaderText = "Normal";
col1.Items.Add("");
col1.Items.Add("0");
col1.Items.Add("1");
col1.Items.Add("2");
dataGridView1.Columns.Insert(5, col1);

The other now unnecessary column I'll hide with .Visible = false; I want the ComboBoxColumn to display database values from that other column that will be hidden and i can't seem to get it to work.

What i have tried:

col1.ValueMember = "il_normal";
col1.DisplayMember = "il_normal";

Those don't work but without error message. This doesn't work and produces error "Values are incorrect"

for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
     dataGridView1.Rows[i].Cells[5].Value = dataGridView1.Rows[i].Cells[7].Value;
}
Matthew
  • 37
  • 8

1 Answers1

0

I found similar question here Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView but solution in that post didn't work for me. In ComboBoxColumn instead of number, I got System.Data.DataRows.(something) so i deduced that it doesn't know which column to use (even though there was only one in DataSource) so i added

col1.DisplayMember = "number";

And then it worked :-)

Matthew
  • 37
  • 8