2

我已将 a 更改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);
    }
}
4

2 回答 2

1

您通常会为已检查的案例找到一个事件。您可以修改该事件中的字符串。

这是有关该事件的更多信息。

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/38f26111-671f-457d-a460-fd5e19b16378

于 2012-11-07T13:18:57.413 回答
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);
            }
        }            
    }
于 2016-08-29T08:46:39.407 回答