告诉 DataGridView 自定义绘画不适合单元格内部

Tell DataGridView that custom paint does not fit inside cell

我绘制单元格的内容,例如图标和文本。如果文本不适合单元格边界,则用省略号绘制。我如何告诉 DataGridView 单元格需要更多 space 来正确处理自动调整大小?

[Customizing Content-based Sizing Behavior](Customizing Content-based Sizing Behavior)中提供了各种选项;我建议您覆盖 DataGridViewCell.GetPreferredSize Method to provide custom cell size data to the specific DataGridViewColumn's CellTemplate Property.

由于您没有详细说明您的特定设计条件,我假设 DataGridViewColumn 是 DataGridViewTextBoxColumn

自定义单元格定义类似于以下内容。

Public Class CustomSizedTextBoxCell : Inherits DataGridViewTextBoxCell

  Protected Overrides Function GetPreferredSize(graphics As Graphics, cellStyle As DataGridViewCellStyle, rowIndex As Int32, constraintSize As Size) As Size
    Dim ret As Size = MyBase.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize)
    ' this simple example just doubles the width of the base preferred size
    ' replace this with logic that satifies your requirements
    ret.Width *= 2
    Return ret
  End Function

End Class

使用此自定义单元格:

DataGridView1.Columns(0).CellTemplate = New CustomSizedTextBoxCell()