Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you Programmatically set DataGridViewComboBoxCell value?
    text
    copied!<p>I'm having trouble setting the value of DataGridViewComboBoxCell. The datagridview column is bound with choices/values but when I attempt either dgv.Rows.Add with specified column values for the comboBoxCells OR setting the cell's value seperately, it generates the "DataGridViewComboBoxCell value is not valid" error. If I add a row with blank values for those column, the choices display in the combo just fine.</p> <p>I have a dialog that is passed an arraylist of a simple object, NDCRecord.</p> <pre><code>public class NDCRecord { public string NDCcode = ""; public string UnitQuantity = ""; public string UnitOfMeasurement = ""; public string Type = ""; public string Number = ""; } </code></pre> <p>In the dialog a datagrid is created programmatically and then is repopulated.</p> <pre><code>public NationalDrugCodesForm(ref ArrayList ndcRecordsIn) { InitializeComponent(); ndcRecords = ndcRecordsIn; SetupDataGridViewColumns(); PopulateForm(); } </code></pre> <p>Setup:</p> <pre><code>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"]); } </code></pre> <p>The AddLine function:</p> <pre><code> 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 }); } </code></pre> <p>The binding functions:</p> <pre><code> 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 &lt; Choices.Length; i++) { if (Choices[i] is object &amp;&amp; Values[i] is object) { dt.Rows.Add(new object[] { Choices[i], Values[i] }); } } cb.DataSource = dt; cb.DisplayMember = "Choice"; cb.ValueMember = "Value"; } } </code></pre> <p>Populate the form:</p> <pre><code> 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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload