2009-09-07 8 views
5

私はDataGridViewを持っていて、そのRowPostPaintイベント中に各行の最初のセルにTreeViewスタイルの点線を描画しています。最初のセル(DataGridViewTextBoxCell)が編集モードにあるとき、線は描画されません。編集コントロールのためのペイントはどうすればできますか?標準の編集コントロールにはPaintイベントがありません。そうしないと新しいタイプのセルを作成したくありません。DataGridViewの編集コントロールの描画をどのように処理するのですか?

+0

私は選択したセルにエクセルのようなグリフを追加しようとしながら、あなたとまったく同じ問題をヒットしました。編集コントロールを使用しているときに、ペイントイベントが呼び出されることはありません。現在、PositionEditingPanelプロパティを使用して編集コントロールを縮小し、親セルのグリフに干渉しないようにしています。私はそれを壊したら私はポストバックします。 – Bryan

+0

@ブライアン:ありがとう。 – Simon

答えて

0

DataGridView.CellPaintingイベントを処理してください。

+0

喜んではいません - 呼び出されますが、セルが編集モードになっているときに何も表示されません。 – Simon

2

カスタムセルタイプを作成し、Bryanが説明したように編集コントロールを縮小することで同様の問題を解決しました。非常に難しいことではありません。編集コントロールをすべての上に描画することを覚えている唯一の方法です。

このような何かがあなたのために働くべき:

public class PaintAccommodatingTextBoxCell : DataGridViewTextBoxCell 
{ 
    // Adjust the editing panel, so that custom painting isn't 
    // drawn over when cells go into edit mode. 
    public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow) 
    { 
     // First, let base class do its adjustments 
     Rectangle controlBounds = base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow); 

     // Shrink the bounds here... 

     return controlBounds; 
    } 
} 

public class PaintAccommodatingTextBoxColumn : DataGridViewTextBoxColumn 
{ 
    PaintAccommodatingTextBoxCell templateCell; 

    public PaintAccommodatingTextBoxColumn() 
    { 
     templateCell = new PaintAccommodatingTextBoxCell(); 
    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return templateCell; 
     } 
     set 
     { 
      PaintAccommodatingTextBoxCell newTemplate = value as PaintAccommodatingTextBoxCell; 
      if (newTemplate == null) 
       throw new ArgumentException("Template must be a PaintAccommodatingTextBoxCell"); 
      else 
       templateCell = newTemplate; 
     } 
    } 
} 
+0

同じ問題に取り組んでいたのですが、単純に戻り値を変更するのではなく、cell.bufferパラメータを変更してbase.PositionEditingPanelを呼び出す必要があることがわかりました。それ以外の場合、EditingControlのサイズは変更されますが、カスタムセルペイントを覆い隠すEditingPanelは変更されません。 – asponge

3

左から16まで、あなたの最初の列の最初のセットセルのパディング、ビューモードまたは編集モードでは、コンテンツが与えられたパディングを使用して表示されますので。

this.dataGridView1.Columns[0].DefaultCellStyle.Padding= new Padding(16,0,0,0); 

その後CellPaintingイベントを処理し、これらの手順を実行します。

  1. は、最初の列だけをペイントし、rowIndexには
  2. は、あなたのツリーラインをペイントしたり、
  3. を好きなレンダリング列のヘッダーを避けるために> = 0でなければなりません
  4. e.Handled = trueを使用してデフォルトペイントをキャンセルします。

ここにコードがあります:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    //Only paint rirst column and RowIndex should be >=0 to avoid rendering column header 
    if (e.ColumnIndex == 0 & e.RowIndex >= 0) 
    { 
     //Paint your tree lines or whatever you want 
     using (var treePen = new Pen(Color.Gray, 1)) 
     { 
      treePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; 
      e.Paint(e.CellBounds, DataGridViewPaintParts.All); 
      e.Graphics.DrawLine(treePen, 
       new Point(e.CellBounds.Left + 4, e.CellBounds.Top), 
       new Point(e.CellBounds.Left + 4, e.CellBounds.Bottom)); 

      e.Graphics.DrawLine(treePen, 
       new Point(e.CellBounds.Left + 4, e.CellBounds.Top + e.CellBounds.Height/2), 
       new Point(e.CellBounds.Left + 12, e.CellBounds.Top + e.CellBounds.Height/2)); 
     } 

     //Cancel default painting using e.Handled = true 
     e.Handled = true; 
    } 
} 

とここにスクリーンショットです:

enter image description here

+1

私はそれを受け入れることができたらいいと思う。 –

関連する問題