How do you programmatically set the value of a DataGridViewComboBoxCell?

I'm having trouble setting the value of a DataGridViewComboBoxCell. The datagridview column is associated with options / values, but when I try to either dgv.Rows.Add with the specified column values ​​for comboBoxCells OR, setting the cell value separately, it generates the error "DataGridViewComboBoxCell is not valid". If I add a row with empty values ​​for these columns, the selection will appear in combo only fine.

I have a dialog that is passed to the arraylist of a simple object, NDCRecord.

public class NDCRecord { public string NDCcode = ""; public string UnitQuantity = ""; public string UnitOfMeasurement = ""; public string Type = ""; public string Number = ""; } 

In the dialog box, a datagrid is created programmatically and then re-populated.

 public NationalDrugCodesForm(ref ArrayList ndcRecordsIn) { InitializeComponent(); ndcRecords = ndcRecordsIn; SetupDataGridViewColumns(); PopulateForm(); } 

Setup:

 private void SetupDataGridViewColumns() { // ----------------------------------------------------- // Add/Del column // ----------------------------------------------------- DataGridViewButtonColumn dgvbcAddRemove = new DataGridViewButtonColumn(); dgvbcAddRemove.HeaderText = "Add"; dgvbcAddRemove.Text = "Add"; dgvbcAddRemove.Name = "Add"; DataGridViewCellStyle addButtonStyle = new DataGridViewCellStyle(); addButtonStyle.BackColor = Color.Blue; addButtonStyle.ForeColor = Color.White; dgvbcAddRemove.HeaderCell.Style = addButtonStyle; dgvNDC.Columns.Add(dgvbcAddRemove); // ----------------------------------------------------- // Additional Columns // ----------------------------------------------------- dgvNDC.Columns.Add("NDCCode", "NDC Code"); dgvNDC.Columns.Add("UnitQuantity", "Unit Quantity"); DataGridViewComboBoxColumn unitOfMeasurement = new DataGridViewComboBoxColumn(); unitOfMeasurement.HeaderText = "Unit Of Measurement"; unitOfMeasurement.Name = "UnitOfMeasurement"; dgvNDC.Columns.Add(unitOfMeasurement); DataGridViewComboBoxColumn type = new DataGridViewComboBoxColumn(); type.HeaderText = "Type"; type.Name = "Type"; dgvNDC.Columns.Add(type); dgvNDC.Columns.Add("Number", "Prescription Number"); AddLine("Del", "", "", "", "", ""); BindUnitOfMeasurement((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["UnitOfMeasurement"]); BindType((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["Type"]); } 

AddLine Function:

  private void AddLine(string buttonLabel, string ndcCode, string unitQuantity, string unitOfMeasurement, string type, string rxNumber) { dgvNDC.Rows.Add(new object[] { buttonLabel, ndcCode, unitQuantity, unitOfMeasurement, type, rxNumber }); } 

Binding Functions:

  private void BindUnitOfMeasurement(DataGridViewComboBoxCell cb) { string[] Values = { "F2", "GR", "ME", "ML", "UN" }; string[] Choices = { "F2 - International Unit", "GR - Gram", "ME - Milligram", "ML - Milliliter", "UN - Unit" }; ControlManip.DataBindDDL(cb, Choices, Values); } private void BindType(DataGridViewComboBoxCell cb) { string[] Values = { "XZ", "VY" }; string[] Choices = { "XZ - Prescription Number", "VY - Link Sequence Number" }; ControlManip.DataBindDDL(cb, Choices, Values); cb.Value = "XZ"; } public static void DataBindDDL(ref ComboBox cb, string[] Choices, string[] Values) { DataTable dt = new DataTable(); dt.Columns.Add("Choice"); dt.Columns.Add("Value"); if (Choices.Length != Values.Length) { throw new Exception("Number of Choices and Values do not match!"); } else { dt.Rows.Add(new object[] { "", "" }); for (int i = 0; i < Choices.Length; i++) { if (Choices[i] is object && Values[i] is object) { dt.Rows.Add(new object[] { Choices[i], Values[i] }); } } cb.DataSource = dt; cb.DisplayMember = "Choice"; cb.ValueMember = "Value"; } } 

Fill the form:

  private void PopulateForm() { if (ndcRecords == null || ndcRecords.Count == 0) return; dgvNDC.Rows.Clear(); foreach(NDCRecord record in ndcRecords) { AddLine("Del", record.NDCcode, record.UnitQuantity, record.UnitOfMeasurement, record.Type, record.Number); } } 
+4
source share
1 answer

The problem was that I was tying individual CELLS instead of COLUMNS, which contained a drop-down list. I had binding methods inserted when the "Add" button was clicked, which made it look as if adding a line with empty values ​​still led to filling out the drop-down lists. But adding a row with the specified values ​​caused an error because the columns of the expanding rows did not have anything in their collection. So, here are the important changes to the code:

In SetupDataGridViewColumns instead

  BindUnitOfMeasurement((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["UnitOfMeasurement"]); BindType((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["Type"]); 

using

 BindUnitOfMeasurement((DataGridViewComboBoxColumn)dgvNDC.Columns["UnitOfMeasurement"]); BindType((DataGridViewComboBoxColumn)dgvNDC.Columns["Type"]); 

and change the binding methods accordingly. Then you can add a line with empty values ​​for combos or given values, and it will work (below one of them has a default value, the other is empty for the user to select)

 AddLine("Del", "", "", "", "XZ", ""); 

Also, I'm not sure if this was necessary, but during the experiments, I also added this for each combo box:

 type.ValueType = typeof (string); 
0
source

All Articles