DEV中BandedGridView 的列 如何设置合并
时间: 2024-03-11 09:48:15 浏览: 188
DataGridView合并指定列单元格.zip_DataGridView合并指定列单元格_attentionsrr_great
在BandedGridView中,可以通过设置BandedGridColumn的Merge属性来实现列合并。具体步骤如下:
1. 在BandedGridView中添加需要合并的列,设置其Merge属性为true。
2. 通过设置BandedGridColumn的GroupIndex属性,将需要合并的列分组。
3. 设置BandedGridView的OptionsView属性,将OptionsView中的ColumnAutoWidth和ColumnAutoHeight属性设置为false,以便手动控制列宽和行高。
4. 在BandedGridView的CustomDrawCell事件中,手动合并单元格,并设置相应的行高和列宽。
下面是一个示例代码:
```
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.RowHandle == gridView1.GetRowHandle(gridView1.DataRowCount - 1)) return; //最后一行不做合并
var column = gridView1.Columns[e.Column.AbsoluteIndex] as DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn;
if (column == null || !column.Merge) return; //当前列不需要合并
var nextRowValue = gridView1.GetRowCellValue(e.RowHandle + 1, column);
var currRowValue = gridView1.GetRowCellValue(e.RowHandle, column);
if (nextRowValue != null && nextRowValue.Equals(currRowValue))
{
e.Handled = true;
return;
}
var band = column.OwnerBand;
var nextRowVisibleIndex = gridView1.GetVisibleIndex(e.RowHandle + 1);
var currRowVisibleIndex = gridView1.GetVisibleIndex(e.RowHandle);
var currRowBottom = e.Bounds.Bottom;
var nextRowTop = gridView1.GetRowCellDisplayRectangle(nextRowVisibleIndex, column).Top;
var rowHeight = nextRowTop - currRowBottom;
e.Handled = true;
using (var backBrush = new SolidBrush(e.Appearance.BackColor))
{
e.Appearance.FillRectangle(backBrush, e.Bounds);
e.Graphics.DrawString(e.DisplayText, e.Appearance.Font, new SolidBrush(e.Appearance.ForeColor), e.Bounds, e.Appearance.GetStringFormat());
}
using (var pen = new Pen(Brushes.Black))
{
if (band != null)
{
var bandWidth = band.Width;
var columnWidth = column.Width;
var bandLeft = gridView1.LeftCoord + band.VisibleIndex * bandWidth;
var columnLeft = bandLeft + column.VisibleIndex * columnWidth;
var columnRight = columnLeft + columnWidth;
if (column.VisibleIndex == band.Columns.Count - 1)
{
columnRight = bandLeft + band.Width;
}
e.Graphics.DrawLine(pen, columnLeft, currRowBottom, columnRight, currRowBottom);
e.Graphics.DrawLine(pen, columnLeft, currRowBottom, columnLeft, currRowBottom + rowHeight);
e.Graphics.DrawLine(pen, columnRight, currRowBottom, columnRight, currRowBottom + rowHeight);
}
}
gridView1.SetRowCellValue(e.RowHandle, column, null);
gridView1.SetRowCellValue(e.RowHandle + 1, column, null);
gridView1.SetRowCellHeight(e.RowHandle, column, rowHeight);
}
```
在上述代码中,我们手动绘制了单元格,并通过判断当前单元格和下一行单元格的值是否相等来判断是否需要合并单元格。同时,我们还手动设置了行高和列宽,以便正确地显示合并后的单元格。
阅读全文