我在 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/