如何触发创建的复选框更改事件c#

时间:2014-06-10 23:37:50

标签: c# events checkbox datagridview

我在gridview中创建了这些复选框,以允许我的用户选择多行。我的问题是,当单击复选框时,他们假设存储每个单击的信息。我在内部放置了断点,从未触发事件。这是为了让这个事件运行我需要调用的东西吗?我的印象就像任何其他事件一样,它只是在触发它时发生。我还没有找到这类问题的通用帮助,似乎是更具体的问题。你有没有参考帮助或有关如何让我的事件正确触发的任何建议?

在表单加载时,这已建立

ckBox = new CheckBox();
//Get the column header cell bounds
Rectangle rect = this.dropdeadGridView.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(18, 18);
//Change the location of the CheckBox to make it stay on the header
ckBox.Location = rect.Location;
ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
//Add the CheckBox into the DataGridView
this.dropdeadGridView.Controls.Add(ckBox);

然后我在这里宣布chkbox_CheckChanged事件

中发生的事情
var rows = dropdeadGridView.Rows;

for (int j = 0; j < this.dropdeadGridView.RowCount; j++)
{
    this.dropdeadGridView[0, j].Value = this.ckBox.Checked;

    bool checkBoxValue = Convert.ToBoolean(dropdeadGridView.Rows[5].Cells[1].Value); 
    if (checkBoxValue)
    {
        values += rows[j].Cells[2] + ",";
        CurrentOrders = values;
    }
}
this.dropdeadGridView.EndEdit();

2 个答案:

答案 0 :(得分:0)

希望您使用的是Windows应用程序

http://csharp.net-informations.com/datagridview/csharp-datagridview-checkbox.htm

此链接告诉我们如何在gridview中添加复选框。更改选择时,将会触发像afteredit,validateedit等网格事件。

我们在那里通过检查列号进行编码(如果列号与复选框列号匹配)执行您在那里事件中提到的操作

我在winforms中的体验较少,而且我只使用了C1FlexGrid(同样也会出现在windows网格中)

答案 1 :(得分:0)

在挂钩事件之前添加解钩事件调用。

ckBox.CheckedChanged -= new EventHandler(ckBox_CheckedChanged);

大多数问题都会得到解决

相关问题