rkulp
asked on
DataGridView ComboBoxCell Editing Problem
I need to be able to edit the contents of a datagridview combobox cell or select one of the items in the dropdown list. If I understand the following statement from MSDN,
(http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx)
I should be able to do it:
The DataGridViewComboBoxCell class is a specialized type of DataGridViewCell used to display a combo box control, which is an editing field combined with a list selection field. The currently selected DataGridViewComboBoxCell hosts a DataGridViewComboBoxEditin gControl in which the user can change the cell's value, assuming the cell's ReadOnly property is set to false.
I have set each cell's ReadOnly value to false using the following code,but it still only allows me to select from the dropdown list.
With dgvStrataBdry
For I = 0 To .RowCount - 1
.CurrentCell = Me.dgvStrataBdry(3, I)
.CurrentCell.ReadOnly = False
Next
End With
Am I misunderstanding that it should allow editing or just doing it wrong.
(http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx)
I should be able to do it:
The DataGridViewComboBoxCell class is a specialized type of DataGridViewCell used to display a combo box control, which is an editing field combined with a list selection field. The currently selected DataGridViewComboBoxCell hosts a DataGridViewComboBoxEditin
I have set each cell's ReadOnly value to false using the following code,but it still only allows me to select from the dropdown list.
With dgvStrataBdry
For I = 0 To .RowCount - 1
.CurrentCell = Me.dgvStrataBdry(3, I)
.CurrentCell.ReadOnly = False
Next
End With
Am I misunderstanding that it should allow editing or just doing it wrong.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Priest04,
Works like a champ. Thanks.
Works like a champ. Thanks.
You are welcome ;)
ASKER
I spoke too quickly. Evidently I don't know how to properly address the column to change the value. I can enter the value in the cell (where I couldn't before) but when I leave the cell (which I neglected to do earler in my rush to get to jury duty) the value returns to one of the previously set items. Apparently I did not set the items correctly. Here is the code. Please show me the correct way.
With dgvStrataBdry
If e.ColumnIndex = 3 Then
dgvStrataBdry.CurrentCell = Me.dgvStrataBdry(e.ColumnI ndex, e.RowIndex)
Select Case .CurrentCell.Value
Case "No"
AuditInfo.CensusFlag(e.Row Index) = False
AuditInfo.NHh(e.RowIndex) = 0
Case "Yes"
AuditInfo.CensusFlag(e.Row Index) = True
AuditInfo.NHh(e.RowIndex) = AuditInfo.Nh(e.RowIndex)
Case Else
AuditInfo.CensusFlag(e.Row Index) = False
AuditInfo.NHh(e.RowIndex) = CType(.CurrentCell.Value, Integer)
Dim cbcol As DataGridViewComboBoxColumn
cbcol = dgvStrataBdry.Columns(3)
cbcol.Items.Add(.CurrentCe ll.Value)
End Select
End If
With dgvStrataBdry
If e.ColumnIndex = 3 Then
dgvStrataBdry.CurrentCell = Me.dgvStrataBdry(e.ColumnI
Select Case .CurrentCell.Value
Case "No"
AuditInfo.CensusFlag(e.Row
AuditInfo.NHh(e.RowIndex) = 0
Case "Yes"
AuditInfo.CensusFlag(e.Row
AuditInfo.NHh(e.RowIndex) = AuditInfo.Nh(e.RowIndex)
Case Else
AuditInfo.CensusFlag(e.Row
AuditInfo.NHh(e.RowIndex) = CType(.CurrentCell.Value, Integer)
Dim cbcol As DataGridViewComboBoxColumn
cbcol = dgvStrataBdry.Columns(3)
cbcol.Items.Add(.CurrentCe
End Select
End If
ASKER
The code above is in:
Private Sub dgvStrataBdry_CellValueCha nged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles dgvStrataBdry.CellValueCha nged
Maybe that is not the correct place for it.
Private Sub dgvStrataBdry_CellValueCha
Maybe that is not the correct place for it.
ASKER
I also have tried the following which I found elsewhere in E-E. It does not work.
Dim cb As New DataGridViewComboBoxCell
cb = DataGridView1(comboboxcolu mnindex, row index)
cb.items.add("xyz")
I don't want to artificially superimpose a text box on top of the cell to capture the information and then put its value in the items. I hope there is a better way. I'm just too dense to see it.
Dim cb As New DataGridViewComboBoxCell
cb = DataGridView1(comboboxcolu
cb.items.add("xyz")
I don't want to artificially superimpose a text box on top of the cell to capture the information and then put its value in the items. I hope there is a better way. I'm just too dense to see it.
ASKER
This seems to work:
Private Sub dgvStrataBdry_EditingContr olShowing( ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewEdi tingContro lShowingEv entArgs) Handles dgvStrataBdry.EditingContr olShowing
If dgvStrataBdry.CurrentCellA ddress.X = 3 Then
CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
End If
End Sub
Private Sub dgvStrataBdry_EditingContr
If dgvStrataBdry.CurrentCellA
CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
End If
End Sub
ASKER