Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't have the VB.Net version,but hopefully this quick C# snippet will help you or point you in the right direction.</p> <p>In this example, I set up a simple DataGridView with 2 columns. The first being a DataGridViewComboBox populated with two choices: "Text" or "Combo".</p> <p>The second column is initially set to DataGridViewTextBoxColumn from the designer.</p> <p>I handle the CurrentCellDirtyStateChanged event on the DataGridView. I check if the cell is dirty and only check the first column (The ComboBox). You gotta call the CommitEdit to get the new value in the combo or else you will be looking at the previous value. Based on the selection in the combo box I then overwrite the cell in the 2nd column with a new cell of that type.</p> <p>You would add your own logic (populate the drop downs and handle the value). You might want to store the value and then put it back into the cell or whatever. </p> <p>Here is the code I used and did a quick and dirty test on:</p> <pre><code>private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dataGridView1.IsCurrentCellDirty == false) { return; } dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); if (dataGridView1.CurrentCell.ColumnIndex == 0) { if (((string)dataGridView1.CurrentCell.Value) == "Text") { dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewTextBoxCell(); } else if (((string)dataGridView1.CurrentCell.Value) == "Combo") { dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewComboBoxCell(); } } } </code></pre> <p>Here is a quick VB translation, that I tested and works.</p> <pre><code>Public Class Form1 Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged If DataGridView1.IsCurrentCellDirty = False Then Return End If DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit) If DataGridView1.CurrentCell.ColumnIndex = 0 Then If CStr(DataGridView1.CurrentCell.Value) = "Text" Then DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewTextBoxCell ElseIf CStr(DataGridView1.CurrentCell.Value) = "Combo" Then DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewComboBoxCell End If End If End Sub </code></pre> <p>End Class</p> <p>You will lose any value stored in that column, so you would need to save it first.</p> <p>Jon</p>
 

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