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

连接单元格:

复制代码
View Code
public class DataGridViewColorLinkCell : DataGridViewLinkCell
{
/// <summary>
/// 是否可见
/// </summary>
public bool Display
{
get;
set;
}

/// <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()
{

DataGridViewColorLinkCell cell =
(DataGridViewColorLinkCell)base.Clone();

cell.Display = this.Display;
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 DataGridViewColorLinkCell()
{
this.Display = true;
this.TopBorderColor = Color.Empty;
this.RightBorderColor = Color.Empty;
this.BottomBorderColor = Color.Empty;
this.LeftBorderColor = Color.Empty;
}

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{

SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}

// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==

DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);

}
if (Display)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, 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);
}
}
}
复制代码


单元格列:

public class DataGridViewColorLinkColumn : DataGridViewLinkColumn
{
public DataGridViewColorLinkColumn()
{
this.CellTemplate = new DataGridViewColorLinkCell();
}
}



posted @   z.seven  阅读(709)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示