首页 > 解决方案 > 在 DataGridView C# 中获取特定的选中单元格

问题描述

我有一个带有复选框列的 datagridview。我编写了一些代码,以便在检查一行时会发生一个过程。但是,代码运行良好,但不读取已检查的特定行,而是沿每一行向下运行。

如何检查是否已检查特定行?

这是我的代码:

void ViewSignature(DataGridView AllContacts)
{
    foreach (DataGridViewRow row1 in AllContacts.Rows)
    {
        try
        {
            if (Convert.ToBoolean(row1.Cells[18].Value = true))
            {
                Process.Start(Properties.Settings.Default.ReferencePoint + @"/Contacts/Contact_" + row1.Cells[0].Value.ToString() + @"/Signature.jpg");
            }
            return;
        }
        catch
        {
            //Error message if the system cannot remove the contact from the database.
            MessageBox.Show("Sorry, something went wrong. Please try again later.\n\nERROR CODE: BINSPIRE1009", "BLUEBERRY INSPIRE 2022", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

这是数据网格视图

请注意,这只是虚假内容,电话号码和电子邮件地址是假的。

在此处输入图像描述

单击上面的按钮后,该过程应该运行。

标签: c#

解决方案


我设法通过使用语句解决了这个问题datagridview.endedit()

我在另一个论坛上读到,当使用按钮时,datagridview 的编辑状态被终止,因为焦点切换到按钮。但是,在使用工具条和菜单条中的项目时情况并非如此,因此需要上述语句来终止 datagridview 的编辑过程,以便工具条可以成功执行其过程。


推荐阅读