C# 从下到上滚动时DataGridView绘制事件未绘制单元格边框
使用C#WinForms,我在表单上停靠(填充)了一个标准DataGridView控件。加载表单时,DataGridView列和行以编程方式创建一次。我在绘制事件中绘制一些标题和单元格边框。一切都很好 我的问题是滚动。当我向下滚动时,一切都很好。但是,当我向上滚动时,一些边框没有绘制。当屏幕上显示未上漆区域时,我再次向下滚动,同时仍保持未上漆区域在屏幕上,它会重新上漆。调试时,我很难看到任何奇怪的东西,因为它完全是随机发生的;现在不能画一排,下次可以画另一排。“我知道它画得很好”,因为当我向下滚动到底部,一直向下,所有的东西都画得像它应该画的那样。只有当我向后滚动时,随机行才不会被绘制,而且每次都可能是不同的行 正如您将在下面的代码中看到的,我正在设置我不希望绘制为透明的边界。即使我将未绘制的边框设置为0,并将边框样式设置为“无”,也会产生相同的结果 这是GUI错误还是刷新错误还是什么 例如,我一直向下滚动,在向下滚动的过程中,我看到所有的边框都画得很好。我可以用同样的结果一遍又一遍地重做它,如下所示 然后,当我向后滚动时,一些边框没有随机绘制,正如您在网格线上看到的文本20:00;行“20:00”的外观应与行“21:00”相同,如下所示 我用于绘制的代码是:C# 从下到上滚动时DataGridView绘制事件未绘制单元格边框,c#,winforms,datagridview,paint,C#,Winforms,Datagridview,Paint,使用C#WinForms,我在表单上停靠(填充)了一个标准DataGridView控件。加载表单时,DataGridView列和行以编程方式创建一次。我在绘制事件中绘制一些标题和单元格边框。一切都很好 我的问题是滚动。当我向下滚动时,一切都很好。但是,当我向上滚动时,一些边框没有绘制。当屏幕上显示未上漆区域时,我再次向下滚动,同时仍保持未上漆区域在屏幕上,它会重新上漆。调试时,我很难看到任何奇怪的东西,因为它完全是随机发生的;现在不能画一排,下次可以画另一排。“我知道它画得很好”,因为当我向下滚
/// <summary>data grid view cell painting event : to draw custom cell borders</summary>
private void dgv_cell_painting(object sender, DataGridViewCellPaintingEventArgs e)
{
try
{
int row = e.RowIndex;
int column = e.ColumnIndex;
int last_col = data_grid_appointments.ColumnCount - 1;
int last_row = data_grid_appointments.RowCount - 1;
string row_tag = ((DataGridView)sender).Rows[row].Tag.ToString();
bool is_hour = (string.IsNullOrEmpty(row_tag) ? false : row_tag.EndsWith("00"));
switch (row < last_row)
{
case true: //not the last row
switch (row == 0
||
is_hour)
{
case true: //first row or row is on the hour
switch (column == -1)
{
case true:
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
);
e.Graphics.DrawString
(
row_tag.Substring(0, 2) + ":" + row_tag.Substring(2)
, e.CellStyle.Font
, new SolidBrush(Color.Black)
, e.CellBounds
, new StringFormat()
{
Alignment = StringAlignment.Center
,
FormatFlags = StringFormatFlags.NoWrap
,
LineAlignment = StringAlignment.Center
}
);
e.Handled = true;
break;
default:
switch (column == data_grid_appointments.ColumnCount - 1)
{
case true:
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
);
e.Handled = true;
break;
default:
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
);
e.Handled = true;
break;
}
break;
}
break;
default: //not the first column
if (column == -1
||
column == data_grid_appointments.ColumnCount - 1)
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
);
e.Handled = true;
}
break;
}
break;
default: //last row
switch (column == -1
||
column == last_col)
{
case true: //row header column OR very last column in the very last row
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
);
e.Handled = true;
break;
default: //not the header column
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
ControlPaint.DrawBorder
(
e.Graphics
, e.CellBounds
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Transparent, 1, ButtonBorderStyle.Solid
, Color.Orange, 1, ButtonBorderStyle.Solid
);
e.Handled = true;
break;
}
break;
}
}
catch (Exception ex)
{}
}
///数据网格视图单元格绘制事件:绘制自定义单元格边框
私有void dgv_单元格_绘制(对象发送器,DataGridViewCellPaintingEventArgs e)
{
尝试
{
int row=e.RowIndex;
int column=e.ColumnIndex;
int last\u col=data\u grid\u accounts.ColumnCount-1;
int last_row=data_grid_appoints.RowCount-1;
字符串row_tag=((DataGridView)sender.Rows[row].tag.ToString();
bool is_hour=(string.IsNullOrEmpty(row_tag)?false:row_tag.EndsWith(“00”);
开关(行<最后一行)
{
case true://不是最后一行
开关(行==0
||
是(小时)
{
case true://第一行或第一行在小时
开关(列==-1)
{
大小写正确:
e、 绘画背景(如CellBounds,真);
e、 绘画内容(如细胞边界);
控制绘图边框
(
e、 图形
,e.CellBounds
,颜色。透明,1,按钮顺序样式。纯色
,颜色。橙色,1,按钮顺序样式。纯色
,颜色。橙色,1,按钮顺序样式。纯色
,颜色。透明,1,按钮顺序样式。纯色
);
e、 拉丝
(
行标记子字符串(0,2)+:“+行标记子字符串(2)
,e.CellStyle.Font
,新的SolidBrush(颜色:黑色)
,e.CellBounds
,新字符串格式()
{
对齐=StringAlignment.Center
,
FormatFlags=StringFormatFlags.NoWrap
,
LineAlignment=StringAlignment.Center
}
);
e、 已处理=正确;
打破
违约:
开关(列==数据\u网格\u约会.ColumnCount-1)
{
大小写正确:
e、 绘画背景(如CellBounds,真);
e、 绘画内容(如细胞边界);
控制绘图边框
(
e、 图形
,e.CellBounds
,颜色。透明,1,按钮顺序样式。纯色
,颜色。橙色,1,按钮顺序样式。纯色
,颜色。橙色,1,按钮顺序样式。纯色
,颜色。透明,1,按钮顺序样式。纯色
);
e、 已处理=正确;
打破
违约:
e、 绘画背景(如CellBounds,真);
e、 绘画内容(如细胞边界);
控制绘图边框
(
private void data_grid_appointments_scroll(object sender, ScrollEventArgs e)
{
((DataGridView)sender).Invalidate();
}