I have a DataGridView
where one of the columns is a DataGridViewComboBoxColumn
. When the grid is populated, that column looks different because of the drop-down arrow appearing on each cell in the column. I'd like to change this so that the drop-down arrow is hidden and only shows up when the row is actually highlighted or when the combobox cell is selected for editing. The behavior I wanted is like how the Properties window in Visual Studio handles its values.
In the DataGridViewComboBoxColumn
, there is a property called DisplayStyle
. Set it to Nothing
to hide the DropDownButton
Further information about the DataGridViewComboBoxDisplayStyle
enumeration is available at this MSDN link
Took me a while to find this, but the above was the answer mixed with a couple of other pages.
This is how to hide the dropdown from a grid based on a value in a different one. The valueToCheck must be in a cell before the one containing the dropdown you wish to hide.
Private Sub dgv_CellPainting(ByVal sender As Object, ByVal e As
DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
'Pages Grid needs to be edited when rendering
If (e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0) Then
Dim valueToCheck = dgv.Rows(e.RowIndex).Cells(2).Value
If (valueToCheck <> "True") Then
Dim thisCol = DirectCast(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell)
thisCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
e.PaintBackground(e.ClipBounds, False)
e.Handled = True
End If
End If
End Sub
如果设置DataGridViewComboBoxColumn.DisplayStyleForCurrentCellOnly = True
,则仅当单元格是当前单元格时才会显示下拉列表。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.