private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//绘制单元格事件
{
// 对第1列相同单元格进行合并
if(e.ColumnIndex == 2 && e.RowIndex != -1 || e.ColumnIndex == 3 && e.RowIndex != -1 || e.ColumnIndex == 4 && e.RowIndex != -1) {
Brush datagridBrush = new SolidBrush(dataGridView1.GridColor);
SolidBrush groupLineBrush = new SolidBrush(e.CellStyle.BackColor);
using(Pen datagridLinePen = new Pen(datagridBrush)) {
// 清除单元格
e.Graphics.FillRectangle(groupLineBrush, e.CellBounds);
if(e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) {
//绘制底边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
// 画右边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else {
// 画右边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
//对最后一条记录只画底边线
if(e.RowIndex == dataGridView1.Rows.Count - 1) {
//绘制底边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
// 填写单元格内容,相同的内容的单元格只填写第一个
if(e.Value != null) {
if(!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())) {
//绘制单元格内容
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
//我唯想不通的是,在绘制直线的时候的"减一"的操作.直接在原来的基础上,什么都不减,也不加,不行么?
{
// 对第1列相同单元格进行合并
if(e.ColumnIndex == 2 && e.RowIndex != -1 || e.ColumnIndex == 3 && e.RowIndex != -1 || e.ColumnIndex == 4 && e.RowIndex != -1) {
Brush datagridBrush = new SolidBrush(dataGridView1.GridColor);
SolidBrush groupLineBrush = new SolidBrush(e.CellStyle.BackColor);
using(Pen datagridLinePen = new Pen(datagridBrush)) {
// 清除单元格
e.Graphics.FillRectangle(groupLineBrush, e.CellBounds);
if(e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) {
//绘制底边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
// 画右边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else {
// 画右边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
//对最后一条记录只画底边线
if(e.RowIndex == dataGridView1.Rows.Count - 1) {
//绘制底边线
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
// 填写单元格内容,相同的内容的单元格只填写第一个
if(e.Value != null) {
if(!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())) {
//绘制单元格内容
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
//我唯想不通的是,在绘制直线的时候的"减一"的操作.直接在原来的基础上,什么都不减,也不加,不行么?
解决方案 »
- 求助 淘宝开放平台的问题 c#
- 2个winForm窗体a,b,关闭b时,让a窗体中dataGridView控件刷新
- C#调用DLL
- //根据直角三角形的2点的坐标求另外1点的坐标(因为我已知的2点是经纬度,并不是平面坐标,)
- GridControl( DevExpress.XtraGrid) 控件使用问题?
- 大家有没发现C#中窗体互访问题
- 这些单词在计算机文章中的中文意思是什么(更新中..)?
- configuration problem in TestApp example of log4net
- 请教:窗体切换显示问题
- 求助,大家帮个忙!感激不敬!
- c# OleDb excel 最后一列无法读取
- 将C#写入excel表格改成写入wps表格好改么?
现在我自己写的,魔方他做的思路.怎么我做的,最后一行多了个这个出来:也就是说,没有数据了,也画矩形出来,我都找不到自己错在哪...
if(e.RowIndex != -1 && e.ColumnIndex > 1) {
SolidBrush gridPen = new SolidBrush(dataGV.GridColor); //网格颜色.
SolidBrush groupPen = new SolidBrush(e.CellStyle.BackColor); //合并单元格的填充色.
using(Pen p = new Pen(gridPen)) { //用Pen画线.
e.Graphics.FillRectangle(groupPen, e.CellBounds);
if(e.RowIndex < dataGV.Rows.Count - 1 && dataGV.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGV.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) {
//单元格值不同时不合并(即加底线).
e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);//这里的减1操作是为了不使继续下一个操作时冲掉之前的网格.
e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); //画右线. }
else {
e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); //画右线.
}
if(e.RowIndex == dataGV.Rows.Count - 1) { //最后一个单元格.
e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); //画底线.
}
if(e.Value != null) {
if(!(e.RowIndex > 0 && dataGV.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())) { //相同值的相邻单元格.
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X, e.CellBounds.Y,StringFormat.GenericDefault);
}
}
}
e.Handled = true;
}