捕获单元格列单击并执行事件

时间:2012-01-29 01:18:23

标签: c# events datagridview cell

我有一个datagridview,其第一列(index = 0)是一个复选框列。我想在选中并取消选中单元格时更新标签。我的函数似乎工作正常,但它不会更新标签,直到该列中的下一个单元格被更改。

当表单加载时,所有行都被选中,所以说4个中的4个。我取消选中一行,标签不会更新。我取消选中另一行然后它说3,所以你看它落后了一步。

我尝试了一些不同的DataGridViewCellEvent,如CellValueChanged,CellStateChanged,CellEndEdit,但它们都采用了这种方式。我仍然需要一个DataGridViewCellEventArgs e,所以我可以检查列。

有什么建议吗?

private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                int numberOfFiles = 0;
                for (int i = 0; i < fileGrid.Rows.Count; i++)
                {
                    Console.WriteLine(fileGrid[0, i].Value.ToString());
                    if (fileGrid[0, i].Value.ToString() == "True")
                    {
                        numberOfFiles++;
                    }
                }
                numberOfFilesLabel.Text = numberOfFiles.ToString();
            }
        }

我还不能回答我自己的问题,但这是我过去所做的:

private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                int numberOfFiles = 0;
                for (int i = 0; i < fileGrid.Rows.Count; i++)
                {
                    Console.WriteLine(fileGrid[0, i].Value.ToString());
                    if (fileGrid[0, i].Value.ToString() == "True")
                    {
                        numberOfFiles++;
                    }
                }

                if (fileGrid.IsCurrentCellDirty)
                    fileGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);

                if (fileGrid.CurrentCell.Value.ToString().Equals("True"))
                {

                    numberOfFiles++;
                }
                if (fileGrid.CurrentCell.Value.ToString().Equals("False"))
                {

                    numberOfFiles--;
                }
                numberOfFilesLabel.Text = numberOfFiles.ToString();
            }
        }

1 个答案:

答案 0 :(得分:3)

发生这种情况是因为更改复选框后未直接提交已编辑的值。当你离开编辑时,它已经承诺了。 您必须立即提交值以获得所需的行为。 我发现的一种方式,但没有尝试是这一个:

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
        if (e.ColumnIndex == 0)
        {
            if (dataGridView2.IsCurrentCellDirty)
                dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit);

            if (dataGridView2.CurrentCell.Value.ToString().Equals("True"))
            {
                MessageBox.Show("Now do the job while checked value changed to True.");
                //
                // Do the job here.
                //
            }
        }
}

编辑:

演示解决方案。

public partial class Form1 : Form
{
    class MyClass
    {
        public bool Check { get; set; }
        public string Name { get; set; }
    }

    public Form1()
    {
        InitializeComponent();

        List<MyClass> lst = new List<MyClass>();
        lst.AddRange(new[] { new MyClass { Check = false, Name = "item 1" }, new MyClass { Check = false, Name = "item 2" } });
        dataGridView1.DataSource = lst;
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            if (dataGridView1.IsCurrentCellDirty)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

            if (dataGridView1.CurrentCell.Value.ToString().Equals("True"))
            {
                MessageBox.Show("Now do the job while checked value changed to True.");
                //
                // Do the job here.
                //
            }
        }

    }
}
相关问题