2009-08-26 16 views
0

DataGridViewにバインドされたデータであるオブジェクト( 'TrackedSet'タイプ)のリストがあります。新しい行が追加されると、データバインドされたオブジェクトのプロパティに基づいて行の外観を変更できるようにしたいと考えています。C#DefaultCellStyleを常に変更せずに、完全な行に特定のセルスタイルを設定する方法

RowPrePaintイベントで行のカスタム背景をペイントできますが、その行の前景色を変更しようとしています。

以下のコードから、行のDefaultCellStyleプロパティをforecolourが好きなように設定されている定義済みセルスタイルに変更しています。

これは正しいですか?それはちょっぴりとした、ちょっと間違っているようだ。

アドバイスありがとうございます。

/// <summary> 
     /// Handles the drawing of each DataGridView row depending on TrackedSet error or flagged state. 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void setLogDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 
     { 
      if ((e.State & DataGridViewElementStates.Displayed) == DataGridViewElementStates.Displayed) 
      { 
       // Always disable normal painting of selection background and focus. 
       e.PaintParts &= ~(DataGridViewPaintParts.SelectionBackground | 
            DataGridViewPaintParts.Focus); 

       // Get the tracked set associated with the row being painted. 
       TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet; 

       if (set.IsFlagged || set.IsError) // Row requires custom painting. 
       { 
        Color backColour1 = Color.Empty; 
        Color backColour2 = Color.Empty; 

        // Get rectangle of area to paint with custom brush. 
        Rectangle rowBounds = new Rectangle(
         e.RowBounds.Left + 1,         // Location x 
         e.RowBounds.Top,          // Location y 
         this.setLogDataGridView.Columns.GetColumnsWidth(
          DataGridViewElementStates.Visible) - 
         this.setLogDataGridView.HorizontalScrollingOffset + 1, // Width 
         e.RowBounds.Height);         // Height 

        // Disable painting of backgrounds when custom painting row. 
        e.PaintParts &= ~(DataGridViewPaintParts.Background | 
             DataGridViewPaintParts.ContentBackground); 

        if (set.IsFlagged) // Highlight colour. 
        { 
         backColour1 = this._highlightBackColour1; 
         backColour2 = this._highlightBackColour2; 
         this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._highlightStyle; 
        } 
        else // Error colour. 
        { 
         backColour1 = this._errorBackColour1; 
         backColour2 = this._errorBackColour2; 
         this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._errorStyle; 
        } 

        // Paint the custom background. 
        using (Brush lgb = new LinearGradientBrush(rowBounds, backColour1, backColour2, LinearGradientMode.Vertical)) 
        { 
         e.Graphics.FillRectangle(lgb, rowBounds); 
        } 
       } 
      } 
     } 

答えて

1

CellFormattingイベントで背景色と前景色を設定できます。

void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet; 
    if (set.IsFlagged) 
    { 
     e.CellStyle.BackColor = Color.Blue; 
     e.CellStyle.ForeColor = Color.White; 
    } 
    else if (set.IsError) 
    { 
     e.CellStyle.BackColor = Color.Red; 
     e.CellStyle.ForeColor = Color.Blue; 
    } 
} 

カスタムグラジエント型のバックグラウンドを持っているあなたはおそらくまだRowPrePaintイベントでそれを行う必要がありますが、CellFormattingイベントでForeColorを設定しようとするだろう。

DataGridViewにバインドした直後に行のDefaultCellStyleを設定してみることもできますが、特定のイベントによってDefaultCellStyleがRowTemplateにリセットされると考えられます。セルスタイルがリセットされないようにデータをグリッドにバインドした後、RowTemplateでDefaultCellStyleを設定することができます。

+0

ありがとうございました。あなたのコードを*ほとんど*動作させていただきありがとうございます。カスタムのforecolorを必要とする追加された各行は、最初はDefaultCellStyleの色で描画され、次にグリッドをスクロールするときにカスタム色を取得します(再描画されたように)。 (私のアプリが動くにつれて、行が定期的に追加されるようになっています)私は、グラデーションペイントを動作させることによってハードなことをやったと思いました! :( – Andy

+0

Andy。私はこのようなコードをよく使用していますが、通常はうまくいきますが、場合によっては適切な背景色が設定されていない単一のセルが表示されます。データグリッドビューをリフレッシュするか、行を追加した後に行を無効にして、正しく描画されるかどうかを確認してください。 –

+0

この回答を受け入れるとマークしています。 .CellStyleプロパティは、BackColorとSelectedBackColorが同じに設定されています。グラデーションバックグラウンドを犠牲にすることにしましたが、これは私の後の効果をもたらし、行全体ではなく1つのセルを着色します。 – Andy

関連する問題