DataGridView单元格ComboBox选值后无法替换为TextBox的问题求助
Hey there! Let's fix that issue where your DataGridView ComboBox isn't switching to a TextBox after selection.
First, let's break down why your current code isn't working: you're trying to assign a DataGridViewTextBoxCell to DataGridView1.CurrentCell.Value, but the Value property is for storing the cell's data—not the cell object itself. That's why you don't see any visual change; you're just setting a value to an object instead of replacing the cell type.
Here's the step-by-step solution to get this working:
1. Correctly Bind the ComboBox Selection Event
First, make sure you're setting up the ComboBox's SelectedIndexChanged event properly in the EditingControlShowing event (this is where you typically customize the editing control for DataGridView cells):
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then Dim comboBox As ComboBox = TryCast(e.Control, ComboBox) If comboBox IsNot Nothing Then ' Remove existing handler first to avoid duplicate triggers RemoveHandler comboBox.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged ' Clear old items and add new ones comboBox.Items.Clear() comboBox.Items.AddRange({1, 2}) ' Attach the selection change handler AddHandler comboBox.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged End If End If End Sub
2. Replace the Cell with a TextBox on Selection
Now, update your selection handler to properly replace the cell type instead of modifying the Value property:
Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Dim combo1 As ComboBox = CType(sender, ComboBox) If combo1.SelectedIndex = 0 Then ' First, end the current editing session—you can't replace a cell while it's being edited DataGridView1.EndEdit() ' Get the current cell's position Dim colIndex = DataGridView1.CurrentCell.ColumnIndex Dim rowIndex = DataGridView1.CurrentCell.RowIndex ' Create a new TextBox cell and set its value Dim textBoxCell As New DataGridViewTextBoxCell() textBoxCell.Value = "S" ' Replace the original ComboBox cell with the new TextBox cell DataGridView1.Rows(rowIndex).Cells(colIndex) = textBoxCell ' Optional: Immediately enter edit mode on the new TextBox if needed DataGridView1.BeginEdit(True) End If End Sub
Key Notes to Remember
- Always call
EndEdit()first: You can't modify the cell type while the cell is in edit mode—this is a common pitfall! - Replace the cell object, not the value: Instead of setting
CurrentCell.Value, you need to overwrite the cell in theCellscollection with your newDataGridViewTextBoxCell. - Avoid duplicate event bindings: Removing and re-adding the
SelectedIndexChangedhandler prevents the event from firing multiple times as you edit different cells.
If your DataGridView is bound to a data source, you might need to adjust this logic to work with your data model, but for unbound grids, this should solve your problem perfectly.
内容的提问来源于stack exchange,提问作者harizh

