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

复选框单元格:

View Code
/// <summary>
/// 表格控件單元格
/// </summary>
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
{
/// <summary>
/// 指示單元格是否可用
/// </summary>
public bool Enabled
{
get;
set;
}
/// <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()
{

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

cell.Enabled = this.Enabled;
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 DataGridViewDisableCheckBoxCell()
{
this.Enabled = true;
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)
{
if (!Enabled)
{
// Paint background
graphics.FillRectangle(Brushes.Silver, cellBounds);
// draw a inactive checkbox
ControlPaint.DrawCheckBox(graphics,
cellBounds.X + this.ContentBounds.X - 2,
cellBounds.Y + this.ContentBounds.Y,
16, 16,
ButtonState.All);

}
else
{
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, cellBounds.Y, cellBounds.X + cellBounds.Width, cellBounds.Y + cellBounds.Height);
}
if (BottomBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(BottomBorderColor), cellBounds.X, cellBounds.Y + cellBounds.Height, cellBounds.X + cellBounds.Width, cellBounds.Y + cellBounds.Height);
}
if (LeftBorderColor != Color.Empty)
{
graphics.DrawLine(new Pen(LeftBorderColor), cellBounds.X, cellBounds.Y, cellBounds.X, cellBounds.Y + cellBounds.Height);
}
}
}

列:

/// <summary>
/// 表格列模板
/// </summary>
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
public DataGridViewDisableCheckBoxColumn()
{
this.CellTemplate = new DataGridViewDisableCheckBoxCell();
}
}


例子下载:

WindowsFormsApplication1.zip

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