自定义DataGridViewCheckBoxCell可视更新在编辑模式下不起作用 [英] Custom DataGridViewCheckBoxCell visual update doesn't work in edit mode
本文介绍了自定义DataGridViewCheckBoxCell可视更新在编辑模式下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 DataGridViewCheckBoxCell
。问题是视觉更新不会在编辑模式下立即发生,仅当我退出它时
I have the following DataGridViewCheckBoxCell
. the problem is the visual update doesn't take place immediately in the edit mode, only when i quit it
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));
}
}
推荐答案
您需要2个修复程序:
-
您还应该创建一个
CustomDataGridViewCheckBoxColumn
其单元格模板设置为您的CustomDataGridViewCheckBoxCell
。
You should create a
CustomDataGridViewCheckBoxColumn
as well which its cell template is set to yourCustomDataGridViewCheckBoxCell
.
而不是 FormattedValue
属性,请使用 formattedValue
参数。
Instead of FormattedValue
property, use formattedValue
parameter.
这是代码:
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));
}
}
这篇关于自定义DataGridViewCheckBoxCell可视更新在编辑模式下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文