我已更改DataGridViewCheckBoxCell
以包含CheckBox右侧反映CheckBox值的字符串。当它是true
时,字符串是“Value1”;当它是false
时,字符串应该变为“Value2”。
字符串值仅在DataGridViewCheckBoxCell
失去焦点后才会更改。我希望在CheckBox值更改后立即更改字符串值。我怎么能这样做?
供参考,这是我个性化的DataGridViewCheckBoxCell
课程:
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);
}
}
答案 0 :(得分:1)
您通常会为已检查的案例找到一个事件。您可以修改该事件中的字符串。
以下是有关此次活动的更多信息。
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/38f26111-671f-457d-a460-fd5e19b16378
答案 1 :(得分:0)
我使用活动CurrentCellDirtyStateChanged
来管理CommitEdit
这是一个示例:
void dgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgv.Columns[dgv.CurrentCell.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxColumn))
{
if (dgv.Columns[dgv.CurrentCell.ColumnIndex].DataPropertyName.StartsWith("_nodb_"))
{
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}