【问题标题】:capture cell column click and perform event捕获单元格列单击并执行事件
【发布时间】:2012-01-29 09:59:40
【问题描述】:

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

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

我尝试了一些不同的 DataGridViewCellEvents,例如 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();
            }
        }

【问题讨论】:

    标签: c# events datagridview cell


    【解决方案1】:

    发生这种情况是因为在更改复选框后未直接提交编辑的值。当您离开编辑器时,它已提交。 您必须立即提交该值才能获得您想要的行为。 我找到但没有尝试的一种方法是:

    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.
                    //
                }
            }
    
        }
    }
    

    【讨论】:

    • 呃刚刚测试了这个,同样的结果,对不起。
    • 好吧,这很奇怪。我刚刚创建了一个演示 sln 并且 sn-p 工作。显示 MessageBox...在您的项目中究竟有什么不工作?
    • 是的,消息框会显示,但是当复选框被选中/取消选中时,我需要增加或减少一个 int - 基本上只是显示检查了多少行。让我尝试重新编写代码以使用您的方法,因为我之前设置的工作方式略有不同
    • k 是的,我首先运行了行数的循环,然后根据当前单元格的点击来增加或减少 int。感谢您的回答,团队为此付出了努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多