博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView重绘代码参考--C#
阅读量:5860 次
发布时间:2019-06-19

本文共 6319 字,大约阅读时间需要 21 分钟。

1、CellFormatting事件,一般重绘单元格属性。
 
private
Bitmap highPriImage;
private
Bitmap mediumPriImage;
private
Bitmap lowPriImage;
private
void
dataGridView1_CellFormatting(
object
sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
//
Set the background to red for negative values in the Balance column.
if
(dataGridView1.Columns[e.ColumnIndex].Name.Equals(
"
Balance
"
))
{
Int32 intValue;
if
(Int32.TryParse((String)e.Value,
out
intValue)
&&
(intValue
<
0
))
{
e.CellStyle.BackColor
=
Color.Red;
e.CellStyle.SelectionBackColor
=
Color.DarkRed;
}
}
//
Replace string values in the Priority column with images.
if
(dataGridView1.Columns[e.ColumnIndex].Name.Equals(
"
Priority
"
))
{
//
Ensure that the value is a string.
String stringValue
=
e.Value
as
string
;
if
(stringValue
==
null
)
return
;
//
Set the cell ToolTip to the text value.
DataGridViewCell cell
=
dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText
=
stringValue;
//
Replace the string value with the image value.
switch
(stringValue)
{
case
"
high
"
:
e.Value
=
highPriImage;
break
;
case
"
medium
"
:
e.Value
=
mediumPriImage;
break
;
case
"
low
"
:
e.Value
=
lowPriImage;
break
;
}
}
}
2、CellPainting事件,一般用于合并单元格用
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.DrawString 自己来“画”。
下面的代码可以对DataGridView第1列内容相同的单元格进行合并:
 
 
#region
"合并单元格的测试"
private
int
?
nextrow
=
null
;
private
int
?
nextcol
=
null
;
private
void
dataGridView1_CellFormatting(
object
sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if
(
this
.dataGridView1.Columns[
"
description
"
].Index
==
e.ColumnIndex
&&
e.RowIndex
>=
0
)
{
if
(
this
.nextcol
!=
null
&
e.ColumnIndex
==
this
.nextcol)
{
e.CellStyle.BackColor
=
Color.LightBlue;
this
.nextcol
=
null
;
}
if
(
this
.nextrow
!=
null
&
e.RowIndex
==
nextrow)
{
e.CellStyle.BackColor
=
Color.LightPink;
this
.nextrow
=
null
;
}
if
(e.RowIndex
!=
this
.dataGridView1.RowCount
-
1
)
{
if
(e.Value.ToString()
==
this
.dataGridView1.Rows[e.RowIndex
+
1
].Cells[e.ColumnIndex].Value.ToString())
{
e.CellStyle.BackColor
=
Color.LightPink;
nextrow
=
e.RowIndex
+
1
;
}
}
}
if
(
this
.dataGridView1.Columns[
"
name
"
].Index
==
e.ColumnIndex
&&
e.RowIndex
>=
0
)
{
if
(e.Value.ToString()
==
this
.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex
+
1
].Value.ToString())
{
e.CellStyle.BackColor
=
Color.LightBlue;
nextcol
=
e.ColumnIndex
+
1
;
}
}
}
//
==========================
//
绘制单元格
private
void
dataGridView1_CellPainting(
object
sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
//
纵向合并
if
(
this
.dataGridView1.Columns[
"
description
"
].Index
==
e.ColumnIndex
&&
e.RowIndex
>=
0
)
{
using
(
Brush gridBrush
=
new
SolidBrush(
this
.dataGridView1.GridColor),
backColorBrush
=
new
SolidBrush(e.CellStyle.BackColor))
{
using
(Pen gridLinePen
=
new
Pen(gridBrush))
{
//
擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
///
/绘制线条,这些线条是单元格相互间隔的区分线条,
///
/因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
if
(e.RowIndex
!=
this
.dataGridView1.RowCount
-
1
)
{
if
(e.Value.ToString()
!=
this
.dataGridView1.Rows[e.RowIndex
+
1
].Cells[e.ColumnIndex].Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom
-
1
,
e.CellBounds.Right
-
1
, e.CellBounds.Bottom
-
1
);
//
下边缘的线
//
绘制值
if
(e.Value
!=
null
)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X
+
2
,
e.CellBounds.Y
+
2
, StringFormat.GenericDefault);
}
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom
-
1
,
e.CellBounds.Right
-
1
, e.CellBounds.Bottom
-
1
);
//
下边缘的线
//
绘制值
if
(e.Value
!=
null
)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X
+
2
,
e.CellBounds.Y
+
2
, StringFormat.GenericDefault);
}
}
//
右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right
-
1
,
e.CellBounds.Top, e.CellBounds.Right
-
1
,
e.CellBounds.Bottom
-
1
);
e.Handled
=
true
;
}
}
}
//
横向合并
if
(
this
.dataGridView1.Columns[
"
name
"
].Index
==
e.ColumnIndex
&&
e.RowIndex
>=
0
)
{
using
(
Brush gridBrush
=
new
SolidBrush(
this
.dataGridView1.GridColor),
backColorBrush
=
new
SolidBrush(e.CellStyle.BackColor))
{
using
(Pen gridLinePen
=
new
Pen(gridBrush))
{
//
擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if
(e.Value.ToString()
!=
this
.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex
+
1
].Value.ToString())
{
//
右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right
-
1
, e.CellBounds.Top,
e.CellBounds.Right
-
1
, e.CellBounds.Bottom
-
1
);
//
绘制值
if
(e.Value
!=
null
)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X
+
2
,
e.CellBounds.Y
+
2
, StringFormat.GenericDefault);
}
}
//
下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom
-
1
,
e.CellBounds.Right
-
1
, e.CellBounds.Bottom
-
1
);
e.Handled
=
true
;
}
}
}
}
#endregion

 DataGridView表头重绘

DataGridView的表头不是很好看,或者有时和整体界面不是很协调,于是找了去查了一些资料,发现可以重绘DataGridView的表头。

2011050614111369.png

代码如下:

 
protected
override
void
OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base
.OnCellPainting(e);
if
(e.ColumnIndex
==
-
1
&&
e.RowIndex
==
-
1
)
{
using
(LinearGradientBrush brush
=
new
LinearGradientBrush(e.CellBounds, Color.LightGray,
Color.White, LinearGradientMode.ForwardDiagonal))
{
e.Graphics.FillRectangle(brush, e.CellBounds);
Rectangle border
=
e.CellBounds;
border.Offset(
new
Point(
-
1
,
-
1
));
e.Graphics.DrawRectangle(Pens.Gray, border);
}
e.PaintContent(e.CellBounds);
e.Handled
=
true
;
}
else
if
(e.RowIndex
==
-
1
)
{
//
标题行
using
(LinearGradientBrush brush
=
new
LinearGradientBrush(e.CellBounds, Color.LightGray,
Color.White, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, e.CellBounds);
Rectangle border
=
e.CellBounds;
border.Offset(
new
Point(
-
1
,
-
1
));
e.Graphics.DrawRectangle(Pens.Gray, border);
}
e.PaintContent(e.CellBounds);
e.Handled
=
true
;
}
else
if
(e.ColumnIndex
==
-
1
)
{
//
标题列
using
(LinearGradientBrush brush
=
new
LinearGradientBrush(e.CellBounds, Color.LightGray,
Color.White, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, e.CellBounds);
Rectangle border
=
e.CellBounds;
border.Offset(
new
Point(
-
1
,
-
1
));
e.Graphics.DrawRectangle(Pens.Gray, border);
}
e.PaintContent(e.CellBounds);
e.Handled
=
true
;
}
}

转载地址:http://alejx.baihongyu.com/

你可能感兴趣的文章
Web前端-Ajax基础技术(下)
查看>>
支配vue框架初阶项目之博客网站-注册页面-单选按钮
查看>>
小tips-一种移动端模拟实现返回拦截的方案
查看>>
刨根问底区块链 —— 基础篇
查看>>
3 jQuery学习笔记第三节 Jq的设计思想之写法
查看>>
swift GCD 的一些高级用法
查看>>
【拒绝一问就懵】之你多少要懂点内存回收机制
查看>>
【深入浅出express】- express入门01
查看>>
Safari无痕模式下,storage被禁用问题
查看>>
旧版Windows远程桌面服务存在漏洞 恐让黑客有机可趁
查看>>
es6-let与const
查看>>
微信小程序修改switch组件大小
查看>>
BCH测试网上出现第一个UTXO证明
查看>>
php 直接调用svn命令
查看>>
30. C# -- 反射(2)
查看>>
接口测试总结
查看>>
Python基础(list类)
查看>>
Android实现简单的电子词典
查看>>
Lesson 9
查看>>
产品管理:启示录 - 特约客户、产品验证、原型测试
查看>>