I have changed a DataGridViewCheckBoxCell to contain a string at the right of the CheckBox that reflects the CheckBox value. When it’s true, the string is “Value1”; when it’s false the string should change to “Value2”.
The string value is changing only after the DataGridViewCheckBoxCell loses focus. I want the string value to change right after the CheckBox value changes. How can I do that?
For reference, this is my personalized DataGridViewCheckBoxCell class:
public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell
{
public static string TRUE_VALUE = "Excitada";
public static string FALSE_VALUE = "Monitorada";
public MyDGVCheckBoxCell(bool isExcitada)
{
FalseValue = FALSE_VALUE;
TrueValue = TRUE_VALUE;
Value = isExcitada ? TRUE_VALUE : FALSE_VALUE;
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
Rectangle contentBounds = this.GetContentBounds(rowIndex);
Point stringLocation = new Point();
stringLocation.Y = cellBounds.Y + 4;
stringLocation.X = cellBounds.X + contentBounds.Right + 1;
graphics.DrawString(this.Value.ToString(), Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);
}
}
You will usually find an event for the checked case. You can modify the string in that event.
Here is more information on the event.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/38f26111-671f-457d-a460-fd5e19b16378