GridView的重绘CellPainting何时发生?
以下代码有啥问题,我想在winForm里实现如下功能:合并"项目名称"列,当点击某一项目时,除了第一行,隐藏这项目其余各行。
单在实现上有点问题,查处结果后,显示还行,但点击项目后,显示上有点问题。好像没触发重绘,请高手帮忙效果:(点击项目1后,项目1下的线没画出来)该有的项目2(名称)也没显示
项目1 | SUM
————
项目2 | SUM
————
|某人A
————
|某人B
———————
项目3 | SUM
bool flag = false;
private void DataGridViewCell_Painting(object sender, DataGridViewCellPaintingEventArgs e)
{
//纵向合并
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns["项目名称"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(dgv.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex != dgv.RowCount - 1)
{
if (e.Value.ToString() != dgv.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
flag = false;
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
}
else
{
if (flag == false)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Crimson, e.ClipBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
flag = true;
}
else
{
e.Graphics.DrawString(" ", e.CellStyle.Font, Brushes.Crimson, e.ClipBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom);
if (e.Value != null)
{
e.Graphics.DrawString("", 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 - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}
}
}
以下代码有啥问题,我想在winForm里实现如下功能:合并"项目名称"列,当点击某一项目时,除了第一行,隐藏这项目其余各行。
单在实现上有点问题,查处结果后,显示还行,但点击项目后,显示上有点问题。好像没触发重绘,请高手帮忙效果:(点击项目1后,项目1下的线没画出来)该有的项目2(名称)也没显示
项目1 | SUM
————
项目2 | SUM
————
|某人A
————
|某人B
———————
项目3 | SUM
bool flag = false;
private void DataGridViewCell_Painting(object sender, DataGridViewCellPaintingEventArgs e)
{
//纵向合并
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns["项目名称"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(dgv.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex != dgv.RowCount - 1)
{
if (e.Value.ToString() != dgv.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
flag = false;
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
}
else
{
if (flag == false)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Crimson, e.ClipBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
flag = true;
}
else
{
e.Graphics.DrawString(" ", e.CellStyle.Font, Brushes.Crimson, e.ClipBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
else
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom);
if (e.Value != null)
{
e.Graphics.DrawString("", 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 - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货