以下是VB代码
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' 给MultiRow指定带有Combo的模板。
- Dim combo As New ComboBoxCell()
- combo.Items.Add("101 北京 通州")
- combo.Items.Add("102 天津 河西")
- combo.Items.Add("103 上海 浦东")
- combo.Name = "Combo1"
- Me.gcMultiRow.Template = Template.CreateGridTemplate(New List(Of Cell)() From { _
- combo, _
- New TextBoxCell() _
- })
- ' 处理CellPainting事件。
- AddHandler gcMultiRow.CellPainting, AddressOf gcMultiRow_CellPainting
- End Sub
- Private Sub gcMultiRow_CellPainting(ByVal sender As Object, ByVal e As CellPaintingEventArgs)
- If e.CellName = "Combo1" AndAlso e.FormattedValue IsNot Nothing Then
- Dim oldForeColor As Color = e.CellStyle.ForeColor
- ' 通过设置ForeColor为Transparent来隐藏默认的字符串。
- e.CellStyle.ForeColor = Color.Transparent
- e.CellStyle.SelectionForeColor = Color.Transparent
- e.Paint(e.ClipBounds)
- ' 根据Cell的值计算需要显示的值
- Dim displayStr As String = Nothing
- If e.FormattedValue.Equals("101 北京 通州") Then
- displayStr = "101"
- ElseIf e.FormattedValue.Equals("102 天津 河西") Then
- displayStr = "102"
- ElseIf e.FormattedValue.Equals("103 上海 浦东") Then
- displayStr = "103"
- End If
- ' 使用DrawText方法自己画短字符串。
- TextRenderer.DrawText(
- e.Graphics,
- displayStr,
- e.CellStyle.Font,
- e.CellBounds,
- oldForeColor,
- TextFormatFlags.PreserveGraphicsTranslateTransform Or
- TextFormatFlags.PreserveGraphicsClipping Or
- TextFormatFlags.VerticalCenter)
- e.Handled = True
- End If
- End Sub
复制代码 |