当前位置:文档之家› grid多表头

grid多表头

单独建一个类:
public class DGVHeaderUnite
{
public string rowValue;//重新绘制的文本框内容
public int col1Height, col2Height;//第一行行高,第二行行高

private static SortedList rowSpan = new SortedList();//取得需要重新绘制的单元格
private static SortedList valueList = new SortedList();

///


///
/// DataGridView合并单元格(横向)
///

/// 绘制的DataGridview
/// 绘制单元格的参数(DataGridview的CellPainting事件中参数)
/// 起始单元格在DataGridView中的索引号
/// 结束单元格在DataGridView中的索引号
/// 重新绘制的文本框内容
public void MerageRowSpan(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
if (cellArgs.ColumnIndex < minColIndex || cellArgs.ColumnIndex > maxColIndex) return;

Rectangle rect = new Rectangle();

if (rowSpan[cellArgs.ColumnIndex] == null)
{
//首先判断当前单元格是不是需要重绘的单元格
//保留此单元格的信息,并抹去此单元格的背景
rect.X = cellArgs.CellBounds.X;
rect.Y = cellArgs.CellBounds.Y;
rect.Width = cellArgs.CellBounds.Width;
rect.Height = cellArgs.CellBounds.Height;

rowSpan.Add(cellArgs.ColumnIndex, rect);
valueList.Add(cellArgs.ColumnIndex, cellArgs.Value.ToString());

if (cellArgs.ColumnIndex == maxColIndex)
MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);
}
else
{
IsPostMerage(dgv, cellArgs, minColIndex, maxColIndex);
}
}

///
/// 不是初次单元格绘制
///

///
///
///
///
public void IsPostMerage(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
//比较单元是否有变化
Rectangle rectArgs = (Rectangle)rowSpan[cellArgs.ColumnIndex];
if (rectArgs.X != cellArgs.CellBounds.X || rectArgs.Y != cellArgs.CellBounds.Y
|| rectArgs.Width != cellArgs.CellBounds.Width || rectArgs.Height != cellArgs.CellBounds.Height
|| valueList[cellArgs.ColumnIndex] != cellArgs.Value.ToString())
{
rectArgs.X = cellArgs.CellBounds.X;
rectArgs.Y = c

ellArgs.CellBounds.Y;
rectArgs.Width = cellArgs.CellBounds.Width;
rectArgs.Height = cellArgs.CellBounds.Height;
rowSpan[cellArgs.ColumnIndex] = rectArgs;
valueList[cellArgs.ColumnIndex] = cellArgs.Value.ToString();
}

if (cellArgs.ColumnIndex == maxColIndex)
MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);
//MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);

}

//绘制单元格
private void MeragePrint(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
int width = 0;//合并后单元格总宽度
int height = cellArgs.CellBounds.Height;//单元格高度

for (int i = minColIndex; i <= maxColIndex; i++)
width += ((Rectangle)rowSpan[i]).Width;

//合并单元格的位置信息
Rectangle reBounds = new Rectangle();
reBounds.X = ((Rectangle)rowSpan[minColIndex]).X;
reBounds.Y = ((Rectangle)rowSpan[minColIndex]).Y;
reBounds.Width = width;
reBounds.Height = height;

using (Brush gridBrush = new SolidBrush(dgv.GridColor),
backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor),
lightBrush = new SolidBrush(SystemColors.ControlLightLight)
)
{
//抹去原来的cell背景
cellArgs.Graphics.FillRectangle(backColorBrush, reBounds);

using (Pen gridLinePen = new Pen(gridBrush), lightLinePen = new Pen(lightBrush))
{
// 画出边线
Point blPoint = new Point(reBounds.Left, reBounds.Bottom - 1);//底线左边位置
Point brPoint = new Point(reBounds.Right - 1, reBounds.Bottom - 1);//底线右边位置
cellArgs.Graphics.DrawLine(gridLinePen, blPoint, brPoint);//下边线

Point tlPoint = new Point(reBounds.Left, reBounds.Top);//上边线左边位置
Point trPoint = new Point(reBounds.Right, reBounds.Top);//上边线右边位置
cellArgs.Graphics.DrawLine(gridLinePen, tlPoint, trPoint); //上边线

Point tlPoint2 = new Point(reBounds.Left, reBounds.Top + 1);//上边线左边位置
Point trPoint2 = new Point(reBounds.Right, reBounds.Top + 1);//上边线右边位置
cellArgs.Graphics.DrawLine(lightLinePen, tlPoint2, trPoint2); //上边白线

Point mlPoint = new Point(reBounds.Left + 1, reBounds.Top + col1Height);//中间线左边位置
Point mrPoint = new Point(reBounds.Right - 5, reBounds.Top + col1Height);//中间线右边位置
cellArgs.Graphics.DrawLine(gridLinePen

, mlPoint, mrPoint);//中间线

Point mlPoint2 = new Point(reBounds.Left + 1, reBounds.Top + col1Height + 1);//中间线左边位置
Point mrPoint2 = new Point(reBounds.Right - 5, reBounds.Top + col1Height + 1);//中间线右边位置
cellArgs.Graphics.DrawLine(lightLinePen, mlPoint2, mrPoint2);//中间白线

Point rtPoint1 = new Point(reBounds.Right - 2, reBounds.Top + 5);//右边线顶部位置
Point rbPoint1 = new Point(reBounds.Right - 2, reBounds.Bottom - 5);//右边线底部位置
cellArgs.Graphics.DrawLine(gridLinePen, rtPoint1, rbPoint1); //右边线

Point rtPoint2 = new Point(reBounds.Right - 1, reBounds.Top + 5);//右边线顶部位置
Point rbPoint2 = new Point(reBounds.Right - 1, reBounds.Bottom - 6);//右边线底部位置
cellArgs.Graphics.DrawLine(lightLinePen, rtPoint2, rbPoint2); //右边白线

//计算绘制字符串的位置
SizeF sf = cellArgs.Graphics.MeasureString(rowValue, cellArgs.CellStyle.Font);
float lstr = (width - sf.Width) / 2;
float rstr = (col1Height - sf.Height) / 2;

//画出文本框
if (rowValue != "")
{
cellArgs.Graphics.DrawString(rowValue, cellArgs.CellStyle.Font,
new SolidBrush(cellArgs.CellStyle.ForeColor),
reBounds.Left + lstr,
rstr + 3,
StringFormat.GenericDefault);
}

for (int i = minColIndex; i <= maxColIndex; i++)
{
sf = cellArgs.Graphics.MeasureString(valueList[i].ToString(), cellArgs.CellStyle.Font);
Rectangle rect = (Rectangle)rowSpan[i];

rect.Y += col1Height;
rect.Height = col2Height;

StringFormat strFmt = new StringFormat();
strFmt.Alignment = StringAlignment.Center;
strFmt.LineAlignment=StringAlignment.Center;

if (i > minColIndex)
{
rtPoint1 = new Point(rect.Left - 2, reBounds.Top + col1Height + 3);//右边线顶部位置
rbPoint1 = new Point(rect.Left - 2, reBounds.Bottom - 4);//右边线底部位置
cellArgs.Graphics.DrawLine(gridLinePen, rtPoint1, rbPoint1); //右边线

rtPoint2 = new Point(rect.Left - 1, reBounds.Top + col1Height + 3);//右边线顶部位置
rbPoint2 = new Point(rect.L

eft - 1, reBounds.Bottom - 1 - 4);//右边线底部位置
cellArgs.Graphics.DrawLine(lightLinePen, rtPoint2, rbPoint2); //右边白线
}

cellArgs.Graphics.DrawString(valueList[i].ToString(), cellArgs.CellStyle.Font,
new SolidBrush(cellArgs.CellStyle.ForeColor),
rect,
strFmt);
}

}
cellArgs.Handled = true;
}

}

在程序里这样调用:
private void dgv1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && ((e.ColumnIndex == 2) || (e.ColumnIndex == 3)))
{
e.CellStyle.Font = new Font(dgvPay.DefaultCellStyle.Font, FontStyle.Bold);
e.CellStyle.WrapMode = DataGridViewTriState.True;
DGVHeaderUnite HeaderUnite = new DGVHeaderUnite();
HeaderUnite.rowValue = "预算/结算成本";
HeaderUnite.col1Height = 18;
HeaderUnite.col2Height = 36;
HeaderUnite.MerageRowSpan(dgvPay, e, 2, 3);
}
if (e.RowIndex == -1 && ((e.ColumnIndex == 4) || (e.ColumnIndex == 5) || (e.ColumnIndex == 6)))
{
e.CellStyle.Font = new Font(dgvPay.DefaultCellStyle.Font, FontStyle.Bold);
e.CellStyle.WrapMode = DataGridViewTriState.True;
DGVHeaderUnite HeaderUnite = new DGVHeaderUnite();
HeaderUnite.rowValue = "付款明细";
HeaderUnite.col1Height = 18;
HeaderUnite.col2Height = 36;
HeaderUnite.MerageRowSpan(dgvPay, e, 4, 6);
}
}


你可以试试

相关主题
文本预览
相关文档 最新文档