c# - gridview 上的复选框事件?

标签 c# .net

我在 Windows 应用程序中的 gridview 上有一个复选框列。我希望有人单击复选框后立即发生事件。

我该怎么做?

最佳答案

新答案,因为现在我知道它是 Windows 窗体

首先,您需要将行设置为可编辑,以便用户在复选框中单击,以避免当客户端单击某一行的 CELL 时您会看到。

假设第一个单元格是复选框:

第二个文本...

我的 Form1.cs 代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dgv.DataSource = new testData[] {
            new testData{ CheckBox = true, Name = "One" },
            new testData{ CheckBox = true, Name = "Two" },
            new testData{ CheckBox = false, Name = "Three" },
            new testData{ CheckBox = false, Name = "Four" }            
        };
    }

    private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == 0) // It's the Checkbox Column
        {
            DataGridViewRow dgvr = dgv.Rows[e.RowIndex];
            MessageBox.Show(String.Format("Row {0} was cliked ({1})", (e.RowIndex + 1).ToString(), 
                dgvr.Cells[1].Value));
        }
    }
}

public class testData
{
    public Boolean CheckBox { get; set; }
    public String Name { get; set; }
}

设计...只需将一个DataGridView组件拖入窗口窗体中,命名为dgv,然后在事件中双击事件CellMouseClick

关于c# - gridview 上的复选框事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/461049/

相关文章:

c# - 将 'Type' 传递给通用构造函数

c# - Wpf: 在 Listview 中隐藏 GridViewColumn

c# - 在 C# T4 模板中通过 DTE 访问项目

c# - 从另一个文件访问私有(private)类

.net - .Net Core 中可变数量的 Oauth 身份验证方案

.net - 无法使用 .NET 连接器连接到本地 MySQL 数据库

c# - 沿 3d 向量查找点

c# - 如何约束泛型类型必须有一个采用特定参数的构造函数?

c# - 使用 PLINQ 是否有可能提高效率?

c# - 是否可以在 Socket 和 TcpClient 对象之间进行转换?