Manually add data to datagrid combobox in C#
-
I am trying to fill a combobox that is in a datagrid with values. The code compiles fine, but when it generates the datagrid it gives a error. "System.ArgumentException: DataGridViewComboBoxCell value is not valid". Any ideas what I am doing wrong?
DataGridViewComboBoxCell colComboCell = new DataGridViewComboBoxCell(); colComboCell.Items.Add("0"); colComboCell.Items.Add("1"); colComboCell.Items.Add("2"); colComboCell.Items.Add("3"); dataGridTicketTypes.Rows.Add(EventTicketPrice_ID, colComboCell);
-
I am trying to fill a combobox that is in a datagrid with values. The code compiles fine, but when it generates the datagrid it gives a error. "System.ArgumentException: DataGridViewComboBoxCell value is not valid". Any ideas what I am doing wrong?
DataGridViewComboBoxCell colComboCell = new DataGridViewComboBoxCell(); colComboCell.Items.Add("0"); colComboCell.Items.Add("1"); colComboCell.Items.Add("2"); colComboCell.Items.Add("3"); dataGridTicketTypes.Rows.Add(EventTicketPrice_ID, colComboCell);
You can try the following code assuming that the comboBox column is named valueColumn: DataGridViewColumn valueColumn = new DataGridViewColumn(); dataGridTicketTypes.Columns.Add( valueColumn ); DataGridViewRow row = new DataGridViewRow(); DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); cell.Items.Add("0"); cell.Items.Add("1"); cell.Items.Add("2"); cell.Items.Add("3"); valueColumn.CellTemplate = cell; int rowIndex = dataGridTicketTypes.Rows.Add( row ); row = dataGridTicketTypes.Rows[ rowIndex ]; Hope it helps. If you have any question contact me. :)
-
You can try the following code assuming that the comboBox column is named valueColumn: DataGridViewColumn valueColumn = new DataGridViewColumn(); dataGridTicketTypes.Columns.Add( valueColumn ); DataGridViewRow row = new DataGridViewRow(); DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); cell.Items.Add("0"); cell.Items.Add("1"); cell.Items.Add("2"); cell.Items.Add("3"); valueColumn.CellTemplate = cell; int rowIndex = dataGridTicketTypes.Rows.Add( row ); row = dataGridTicketTypes.Rows[ rowIndex ]; Hope it helps. If you have any question contact me. :)
The code you provided did add the items to a dropdown menu, however it added a new column in which it added this data which I want to specify it as I am adding values. 1. Create a new form 2. Add a datagridview1 to the form 3. add two columns to the data grid (IDColumn, valueColumn) 4. Add a button to the form which will insert a record 5. When you click on the button it should add one row to the DataGrid by using something like below (but that actually works).
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.Items.Add("0");
cell.Items.Add("1");
cell.Items.Add("2");
cell.Items.Add("3");
dataGridView1.Rows.Add("test", cell); -
The code you provided did add the items to a dropdown menu, however it added a new column in which it added this data which I want to specify it as I am adding values. 1. Create a new form 2. Add a datagridview1 to the form 3. add two columns to the data grid (IDColumn, valueColumn) 4. Add a button to the form which will insert a record 5. When you click on the button it should add one row to the DataGrid by using something like below (but that actually works).
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.Items.Add("0");
cell.Items.Add("1");
cell.Items.Add("2");
cell.Items.Add("3");
dataGridView1.Rows.Add("test", cell);You can skip the first two lines : DataGridViewColumn valueColumn = new DataGridViewColumn(); dataGridTicketTypes.Columns.Add( valueColumn );
-
You can skip the first two lines : DataGridViewColumn valueColumn = new DataGridViewColumn(); dataGridTicketTypes.Columns.Add( valueColumn );
I already had those skipped. My code looks like this which still does not add information to the drop down menu.
private void button1_Click(object sender, EventArgs e) { DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); cell.Items.Add("0"); cell.Items.Add("1"); cell.Items.Add("2"); cell.Items.Add("3"); dataGridView1.Rows.Add("test", cell); }