假设在一个表中有几列,分别是id | name | description |
----------------------------
1 | xxx | ddd |
----------------------------
2 |xxx | lll |
----------------------------
3 |yyy |yyy |
----------------------------
那么,我要对横向相同的列NAME,与description做合并,纵向的name列中内容相同的两行做合并,效果是:
id | name | description |
----------------------------
1 | xxx | ddd |
----- |---------------
2 |xxx | lll |
----------------------------
3 |yyy |
----------------------------
在对dataGridView的CellPainting事件处理时,发现实现边框的效果没有问题,但想把合并的单元格的背景色弄成其他颜色突出显示的时候发现问题出来了,在移动横向或纵向滚动条时,合并的单元格有一部分的颜色消失或者本来不应该被绘制颜色的单元格也出现了颜色,所以觉得是绘制的方法存在问题,但检查了下又发现不了问题,觉得是否是因为横向移动或纵向移动滚动条的时候,CELLPAINT的顺序发生了改变造成的呢?请大家帮忙.
代码如下:
private int? nextrow = null;
private int? nextcol = null;
//绘制单元格
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))
{
// 擦除原单元格背景
//绘制线条,这些线条是单元格相互间隔的区分线条,
//因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
if (this.nextrow != null & e.RowIndex ==nextrow)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightPink) , e.CellBounds);
this.nextrow = null;
}
if (this.nextcol != null & e.ColumnIndex == this.nextcol)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds);
this.nextcol = null;
}
else
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
}
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.FillRectangle(new SolidBrush(Color.LightPink), e.CellBounds);
nextrow = e.RowIndex+1;
//nextcol = e.ColumnIndex;
}
}
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);
}
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds);
this.nextcol = e.ColumnIndex +1;
}
//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
} }
}
}
----------------------------
1 | xxx | ddd |
----------------------------
2 |xxx | lll |
----------------------------
3 |yyy |yyy |
----------------------------
那么,我要对横向相同的列NAME,与description做合并,纵向的name列中内容相同的两行做合并,效果是:
id | name | description |
----------------------------
1 | xxx | ddd |
----- |---------------
2 |xxx | lll |
----------------------------
3 |yyy |
----------------------------
在对dataGridView的CellPainting事件处理时,发现实现边框的效果没有问题,但想把合并的单元格的背景色弄成其他颜色突出显示的时候发现问题出来了,在移动横向或纵向滚动条时,合并的单元格有一部分的颜色消失或者本来不应该被绘制颜色的单元格也出现了颜色,所以觉得是绘制的方法存在问题,但检查了下又发现不了问题,觉得是否是因为横向移动或纵向移动滚动条的时候,CELLPAINT的顺序发生了改变造成的呢?请大家帮忙.
代码如下:
private int? nextrow = null;
private int? nextcol = null;
//绘制单元格
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))
{
// 擦除原单元格背景
//绘制线条,这些线条是单元格相互间隔的区分线条,
//因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
if (this.nextrow != null & e.RowIndex ==nextrow)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightPink) , e.CellBounds);
this.nextrow = null;
}
if (this.nextcol != null & e.ColumnIndex == this.nextcol)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds);
this.nextcol = null;
}
else
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
}
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.FillRectangle(new SolidBrush(Color.LightPink), e.CellBounds);
nextrow = e.RowIndex+1;
//nextcol = e.ColumnIndex;
}
}
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);
}
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds);
this.nextcol = e.ColumnIndex +1;
}
//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
} }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货