如何结束单元格的编辑,在单击时更改其内容?

时间:2012-11-07 12:52:09

标签: c# winforms

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

2 个答案:

答案 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);
            }
        }            
    }