FlexGrid合并单元格
--------------------------------------------------------------------------------
在WinForm的开发中,经常会用到FlexGrid控件,用于显示数据和编辑数据,合并单元格也是经常用到的。
1.单元格自动合并
C1FlexGrid 一帮情况下对于各单元格可以在行方向和列方向合并单元格,行或者列的AllowMerging的属性设置为True的时候,相邻的单元格内容如果相同,那么就会自动合并这两个单元格。此例为列方向自动合并。
行方向合并时,针对要合并的行进行设置。如 C1FlexGrid1.Rows(rownm).AllowMerging = True,其中rownm为行号。
另外、AllowMerging的属性设置为Spill (AllowMergingEnum 枚举型)的时候、如果一个单元格内的文字列长度大于单元格的长度时、相邻的单元格 (必须是空白单元格) 。参照【cccccccc】和【dddd】
2.手动指定单元格合并
使用GetCellRange方法可以手动指定需要合并的单元格
代码如下
C1FlexGrid1.AllowMerging = C1.Win.C1FlexGrid.AllowMergingEnum.Custom
Dim cr As C1.Win.C1FlexGrid.CellRange
cr = C1FlexGrid1.GetCellRange(1, 2, 1, 3)
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(2, 1, 3, 2)
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(2, 4, 3, 4)
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(4, 1, 5, 5)
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(1, 6, 5, 7)
C1FlexGrid1.MergedRanges.Add(cr)
3.Header部的单元格合并
同样用GetCellRange方法
代码如下
C1FlexGrid1.AllowMerging = C1.Win.C1FlexGrid.AllowMergingEnum.Custom
Dim cr As C1.Win.C1FlexGrid.CellRange
cr = C1FlexGrid1.GetCellRange(0, 0, 6, 0)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(0, 1, 0, 5)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(1, 1, 1, 2)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(2, 1, 3, 2)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(1, 3, 3, 5)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(5, 1, 6, 1)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
cr = C1FlexGrid1.GetCellRange(4, 2, 6, 2)
cr.StyleNew.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
C1FlexGrid1.MergedRanges.Add(cr)
C1FlexGrid1.Rows(0).Item(0) = "A"
C1FlexGrid1.Rows(0).Item(1) = "B"
C1FlexGrid1.Rows(1).Item(1) = "C"
C1FlexGrid1.Rows(2).Item(1) = "D"
C1FlexGrid1.Rows(1).Item(3) = "E"
C1FlexGrid1.Rows(5).Item(1) = "F"
C1FlexGrid1.Rows(4).Item(2) = "G"
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。