2017-04-05 37 views
0

我試圖自定義繪製DataGridView,但我很難讓它看起來正確後DataGridView滾動。有些線條顯示在我的單元格中。問題在於滾動控件時,繪畫事件似乎不會重繪單元格,因此在窗體邊緣繪製的單元格在滾動到中心時不會重新繪製。問題描繪可滾動datagridview

我可以通過忽略這樣一個事實來消除這個問題:DataGridView邊緣的一部分單元格不顯示,並將它們繪製爲完全可見,但這會使我的應用程序看起來不太好。

我的問題是:有沒有更好的方式來繪製DataGridView,以便我可以調整我的單元格繪畫的部分顯示的單元格?我查看了其他問題,並試圖使用其他繪畫事件,但我一直無法獲得任何工作。

這是一個簡單的程序,演示了我正在談論的問題。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     for (int i = 0; i < 256; i++) 
      dataGridView1.Rows.Add(new object[] { string.Format("Test_text_{0}", i) }); 

     dataGridView1.Paint += DataGridView1_Paint; 
     dataGridView1.CellPainting += (s, e) => { if (e.RowIndex > 0 && e.ColumnIndex >= 0) e.Handled = true; }; 
    } 

    private void DataGridView1_Paint(object sender, PaintEventArgs e) 
    { 
     Rectangle rect; 

     using (Brush gridBrush = new SolidBrush(dataGridView1.GridColor), 
      cellBrush = new SolidBrush(dataGridView1.DefaultCellStyle.BackColor), 
      textBrush = new SolidBrush(dataGridView1.DefaultCellStyle.ForeColor)) 
     { 
      using (Pen gridLinePen = new Pen(gridBrush)) 
      { 
       for (int row = 0; row < dataGridView1.Rows.Count; row++) 
       { 
        if (!dataGridView1.Rows[row].Displayed) 
         continue; 

        if (dataGridView1.Rows[row].Cells[0].Value != null) 
        { 
         //draw cell 
         rect = dataGridView1.GetCellDisplayRectangle(0, row, true);//Setting cutOverflow to false removes lines 
         e.Graphics.FillRectangle(cellBrush, rect); 
         e.Graphics.DrawLine(gridLinePen, rect.Left, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1); 
         e.Graphics.DrawLine(gridLinePen, rect.Right - 1, rect.Top, rect.Right - 1, rect.Bottom); 

         //add text 
         e.Graphics.DrawString(dataGridView1.Rows[row].Cells[0].Value.ToString(), 
          dataGridView1.DefaultCellStyle.Font, textBrush, rect.X + 4, rect.Y + 4); 
        } 
        else 
         //handle empty rows 
         e.Graphics.FillRectangle(gridBrush, dataGridView1.GetCellDisplayRectangle(0, row, true)); 
       } 
      } 
     } 
    } 
} 

設計代碼

partial class Form1 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
     this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // dataGridView1 
     // 
     this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
     this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
     this.Column1}); 
     this.dataGridView1.Location = new System.Drawing.Point(46, 36); 
     this.dataGridView1.Name = "dataGridView1"; 
     this.dataGridView1.Size = new System.Drawing.Size(184, 428); 
     this.dataGridView1.TabIndex = 0; 
     // 
     // Column1 
     // 
     this.Column1.HeaderText = "Column1"; 
     this.Column1.Name = "Column1"; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(281, 522); 
     this.Controls.Add(this.dataGridView1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.DataGridView dataGridView1; 
    private System.Windows.Forms.DataGridViewTextBoxColumn Column1; 
} 

下面是截圖

enter image description here

+1

嘗試使其滾動事件中的DGV無效! – TaW

+0

這是有效的。謝謝! –

+0

或[使用此](http://stackoverflow.com/questions/13999781/tearing-in-my-animation-on-winforms-c-sharp) – TaW

回答

1

問題在於PaintCellPainting事件只繪製了在滾動DataGridView之前未顯示的單元部分。我能夠通過強制整個DataGridView在Scroll事件中重繪來解決問題。

dataGridView1.Scroll += (s, e) => dataGridView1.Invalidate(); 
0

你寫

dataGridView1.CellPainting += (s, e) => 
    { if (e.RowIndex > 0 && e.ColumnIndex >= 0) e.Handled = true; }; 

執行禁止小區畫使用CellPainting事件而不是Paint事件的單元格繪畫。

+0

我只禁用細胞繪畫的細胞,我畫'Paint'事件。我試過使用'CellPainting'事件,除了在單個單元格上使用相同的代碼,但我得到了相同的結果。 (在我的應用程序中,我需要在某些行上連接相鄰列,因此如果可能,我寧願使用'Paint'事件或Pre/Post行繪畫事件之一。) –

+0

也許這個SO解決方案有助於:[ DataGridView CellPainting不完全在滾動上工作](http://stackoverflow.com/a/1145680/880990) –

+1

... orr this [通過處理DataGridView.Paint合併單元格滾動時塗抹](https://social.msdn。 microsoft.com/Forums/windows/en-US/baca9e53-d927-4020-87af-6da08f1c991e/merged-cells-by-handling-datagridviewpaint-smears-when-scrolled?forum=winformsapplications) –