讓我首先說我找到了以下內容,但沒有一個解決方案適用於我。滾動DataGridView將由CellPainting和RowPostPaint事件繪製的任何東西都刪除
Cells gets overlapped and painted when Scroll happens in DataGridView
DataGridView overrides my custom row painting
DataGridView CellPainting Not Fully Working on Scroll
在這兩個CellPainting和RowPostPaint,我使用e.Graphics.DrawLine
當電網加載一切都繪製爲我所期望的。我有兩個問題:
- 當我水平滾動,任何最初沒有顯示在屏幕上沒有我的風俗畫,並
- 當我向後滾動一些已經畫的線已經轉向通過一個單元格的中間創建一條線條,而不是像在卷軸之前那樣在邊框上。
這裏都是我的油漆事件:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
int width = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
width += col.Width;
if (col.Index % 3 == 0)
e.Graphics.DrawLine(Pens.Black, width + 1, e.RowBounds.Top, width + 1, e.RowBounds.Bottom - 1);
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int width = 0;
if (e.RowIndex == -1)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
width += col.Width;
if (col.Index % 3 == 0)
e.Graphics.DrawLine(Pens.Black, width + 1, e.CellBounds.Top +1, width + 1, e.CellBounds.Bottom - 2);
else
e.Graphics.DrawLine(Pens.White, width + 1, e.CellBounds.Top +1, width + 1, e.CellBounds.Bottom - 2);
}
}
}
更新的代碼 因爲我已經結合我漆事件逼到CellPainting
事件,並取得了一定的變化由TaW建議。仍然沒有工作,但我覺得我現在只是缺少一些小東西。
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1)
{
if (e.ColumnIndex % 3 == 0)
e.Graphics.DrawLine(Pens.Black, e.CellBounds.Right, e.CellBounds.Top + 1, e.CellBounds.Right, e.CellBounds.Bottom - 2);
else
e.Graphics.DrawLine(Pens.White, e.CellBounds.Right, e.CellBounds.Top + 1, e.CellBounds.Right, e.CellBounds.Bottom - 2);
}
else
if (e.ColumnIndex % 3 == 0)
e.Graphics.DrawLine(Pens.Black, e.CellBounds.Right, e.CellBounds.Top + 1, e.CellBounds.Right, e.CellBounds.Bottom - 2);
}
您需要爲__both__ x __and__ y使用相對值!因此,使c.CellBounds.Left而不是寬度.. AlsO:你能顯示一個截圖嗎? – TaW
這絕對有道理!不過出於某種原因,當我做出這種改變時,根本沒有任何東西被繪製。我可以看到_should_在那裏間歇閃爍的線條。 – Sam07
對不起,我應該馬上注意到:Cell_Parameter事件被調用__each__ Cell,所以循環是錯誤的! – TaW