2017-01-16 4 views
1

カスタムを作成しましたDataGridViewTextBoxCellインスタンス。それらのほとんどで私はカスタムForeColorの値を設定し、それは正常に動作します。しかし、SelectionModeFullRowSelectの場合、このセルの値はForeColorに優先します。DataGridViewカスタムセルは、選択したときに通常のForeColorを使用する必要があります

セルがセルにされても動作しないときにDrawイベントで設定しようとしました。

私のセルは次のように定義されています。

public class CustomCell : DataGridViewTextBoxCell 
{ 
    protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) 
    { 
     if (string.IsNullOrEmpty(value.ToString())) 
     { 
      return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context); 
     } 

     if (value.ToString().Contains("test")) 
     { 
      cellStyle.ForeColor = Color.Blue; 
     } 
     return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context); 
    } 
} 

Iは、選択モードを変更する必要はありませんが、私はその権利ForeColorが、選択BackColorと、このセルを表示したいです。

このソリューションはどのように見えるのですか?

答えて

1

あなたは、セルのPaintメソッドをオーバーライドし、ForeColorの同じ色にcellStyle.SelectionForeColorを設定することができます。

protected override void Paint(Graphics graphics, Rectangle clipBounds, 
    Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, 
    object value, object formattedValue, 
    string errorText, DataGridViewCellStyle cellStyle, 
    DataGridViewAdvancedBorderStyle advancedBorderStyle, 
    DataGridViewPaintParts paintParts) 
{ 
    if (string.Format("{0}", formattedValue) == "something") 
    { 
     cellStyle.ForeColor = Color.Red; 
     cellStyle.SelectionForeColor = cellStyle.ForeColor; 
    } 
    base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, 
     formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 
} 

注:カスタムセルを作成せずにDataGridViewの同じ使用CellFormattingまたはCellPaintingイベントを行うことができます。

+0

ありがとう、それは動作します。私はイベントを使うことができることを知っています。しかし、実際に特別なルールを適用して同じように見えるはずのプログラム全体にたくさんのセルがあります。このためにカスタムセルを作る方がよかったです。 – Booser

+0

あなたはより良い要求を知っている、私はあなたの情報や将来の読者のためだけにそれを共有:) –

関連する問題