C#DataGridView单元格边框颜色问题

表头单元格:

View Code
/// <summary>
/// 表格列头部单元格
/// </summary>
public class DataGridViewColorColumnHeaderCell : DataGridViewColumnHeaderCell
{
/// <summary>
/// 顶部边框颜色
/// </summary>
public Color TopBorderColor
{
get;
set;
}
/// <summary>
/// 右侧边框颜色
/// </summary>
public Color RightBorderColor
{
get;
set;
}
/// <summary>
/// 下部边框颜色
/// </summary>
public Color BottomBorderColor
{
get;
set;
}
/// <summary>
/// 左侧边框颜色
/// </summary>
public Color LeftBorderColor
{
get;
set;
}

// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewColorColumnHeaderCell cell =
(DataGridViewColorColumnHeaderCell)base.Clone();
cell.TopBorderColor = this.TopBorderColor;
cell.RightBorderColor = this.RightBorderColor;
cell.BottomBorderColor = this.BottomBorderColor;
cell.LeftBorderColor = this.LeftBorderColor;
return cell;
}

// By default, enable the button cell.
public DataGridViewColorColumnHeaderCell()
{
this.TopBorderColor = Color.Empty;
this.RightBorderColor = Color.Empty;
this.BottomBorderColor = Color.Empty;
this.LeftBorderColor = Color.Empty;
}

protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
//绘制边框颜色
if (TopBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(TopBorderColor), cellBounds.X, cellBounds.Y, cellBounds.X + cellBounds.Width, cellBounds.Y);
}
if (RightBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(RightBorderColor), cellBounds.X + cellBounds.Width-1, cellBounds.Y, cellBounds.X + cellBounds.Width-1, cellBounds.Y + cellBounds.Height);
}
if (BottomBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(BottomBorderColor), cellBounds.X, cellBounds.Y + cellBounds.Height-1, cellBounds.X + cellBounds.Width, cellBounds.Y + cellBounds.Height-1);
}
if (LeftBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(LeftBorderColor), cellBounds.X, cellBounds.Y, cellBounds.X, cellBounds.Y + cellBounds.Height);
}
}
}

使用方法:
DataGridViewColorColumnHeaderCell hc = new DataGridViewColorColumnHeaderCell();
hc.Value = "数值";//表头文本
hc.TopBorderColor = Color.Red;
hc.LeftBorderColor = Color.Red;
hc.RightBorderColor = Color.Red;
dataGridView1.Columns[1].HeaderCell = hc;

posted @ 2012-02-15 09:19  z.seven  阅读(2144)  评论(0编辑  收藏  举报