Reputation: 596
I made some custom DataGridViewTextBoxCell
instances. In most of them I set custom ForeColor
values and that works fine. But when the SelectionMode
is FullRowSelect
it overrides my ForeColor
values for this cell.
I tried to set it in the Draw event when the cell is selcted but it does not work.
My cell is definde as follows.
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 don't want to change the selection mode but I want to show this cell with its right ForeColor
but the selection BackColor
.
How could the solution to this look like?
Upvotes: 1
Views: 43
Reputation: 125277
You can override Paint
method of cell and set cellStyle.SelectionForeColor
to the same color of ForeColor
:
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);
}
Note: You can do the same using CellFormatting
or CellPainting
event of DataGridView
without creating a custom cell.
Upvotes: 1