seleccionar - usar checkbox en datagridview c#
La actualizaciĆ³n visual personalizada DataGridViewCheckBoxCell no funciona en el modo de ediciĆ³n (1)
Necesitas 2 soluciones:
También debe crear una
CustomDataGridViewCheckBoxColumn
cuya plantilla de celda esté configurada enCustomDataGridViewCheckBoxCell
.En lugar de la propiedad
FormattedValue
, use el parámetroformattedValue
.
Aquí está el código:
public class CustomDataGridViewCheckBoxColumn: DataGridViewCheckBoxColumn
{
public CustomDataGridViewColumn()
{
this.CellTemplate = new CustomDataGridViewCheckBoxCell();
}
}
public class CustomDataGridViewCheckBoxCell: DataGridViewCheckBoxCell
{
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, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
var val = (bool?)formattedValue;
var img = val.HasValue && val.Value ? Properties.Resources.Checked : Properties.Resources.UnChecked;
var w = img.Width;
var h = img.Height;
var x = cellBounds.Left + (cellBounds.Width - w) / 2;
var y = cellBounds.Top + (cellBounds.Height - h) / 2;
graphics.DrawImage(img, new Rectangle(x, y, w, h));
}
}
Tengo el siguiente DataGridViewCheckBoxCell
. el problema es que la actualización visual no tiene lugar inmediatamente en el modo de edición, solo cuando lo dejo
public class CustomDataGridViewCell : DataGridViewCheckBoxCell
{
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, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
var val = (bool?)FormattedValue;
var img = val.HasValue && val.Value ? Properties.Resources._checked : Properties.Resources._unchecked;
var w = img.Width;
var h = img.Height;
var x = cellBounds.Left + (cellBounds.Width - w) / 2;
var y = cellBounds.Top + (cellBounds.Height - h) / 2;
graphics.DrawImage(img, new Rectangle(x, y, w, h));
}
}