2017-03-07 23 views
0

讓我首先說我找到了以下內容,但沒有一個解決方案適用於我。滾動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當電網加載一切都繪製爲我所期望的。我有兩個問題:

  1. 當我水平滾動,任何最初沒有顯示在屏幕上沒有我的風俗畫,並
  2. 當我向後滾動一些已經畫的線已經轉向通過一個單元格的中間創建一條線條,而不是像在卷軸之前那樣在邊框上。

這裏都是我的油漆事件:

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); 
} 
+1

您需要爲__both__ x __and__ y使用相對值!因此,使c.CellBounds.Left而不是寬度.. AlsO:你能顯示一個截圖嗎? – TaW

+0

這絕對有道理!不過出於某種原因,當我做出這種改變時,根本沒有任何東西被繪製。我可以看到_should_在那裏間歇閃爍的線條。 – Sam07

+0

對不起,我應該馬上注意到:Cell_Parameter事件被調用__each__ Cell,所以循環是錯誤的! – TaW

回答

1

你缺少了幾步適當CellPainting

您需要

  • 告訴你是完成用它,否則一切都將被覆蓋的情況下(1)
  • 因此,您還需要確保..
    • The Backgound(2)and ..
    • The Content(3)are drawn。

下面是一個例子:

private void dataGridView1_CellPainting(object sender, 
              DataGridViewCellPaintingEventArgs e) 
    { 
     Rectangle cr = e.CellBounds; 
     e.PaintBackground(cr, true); // (2) 
     e.PaintContent(cr);   // (3) 

     if (e.ColumnIndex % 2 == 0) 
     { 
       e.Graphics.DrawLine(Pens.LawnGreen, cr.Right-2, cr.Top + 1, 
                cr.Right-12, cr.Bottom - 2); 
     } 

     e.Handled = true; // (1) 
    } 

你可以改變事物的順序,只要確保後臺先畫!

請注意,與評論相反,不需要使用InvalidateScroll事件進行編碼。

+0

這適用於我,但如果我嘗試繪製細胞分隔線的確切位置,我的線條不可見!我可以在任何地方繪畫,它顯示很好。任何想法爲什麼? - 編輯我是一個doofus,我試圖在界限之外繪畫....謝謝! – Sam07