选中某个复选框后如何执行代码

时间:2011-10-20 13:45:42

标签: c# .net winforms events datagridview

我有一个带有多个复选框的datagridview。选中Finished复选框后,我需要执行linq代码来更新特定的表。如何确定特定复选框是否脏,以及在何处编写代码以传递我需要传递给表的值。请注意,它与datagridview所基于的表不同。

感谢。

编辑:

 private void propertyInformationDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)propertyInformationDataGridView.Rows[e.RowIndex].Cells[3];

        DataGridViewRow row = propertyInformationDataGridView.Rows[e.RowIndex] as DataGridViewRow;

        System.Data.DataRowView SelectedRowView;
        newCityCollectionDataSet.PropertyInformationRow SelectedRow;

        SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
        SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;

        if (Convert.ToBoolean(checkCell.EditedFormattedValue) == true)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var matchedCaseNumber = (from c in dc.GetTable<PropertyInformation>()
                                     where c.CaseNumberKey == SelectedRow.CaseNumberKey
                                     select c).SingleOrDefault();

            reportsSent newReport = new reportsSent();
            newReport.CaseNumberKey = SelectedRow.CaseNumberKey;
            dc.reportsSents.InsertOnSubmit(newReport);
            dc.SubmitChanges();

        }
    }

我是否需要在某个时候结束这个问题?

2 个答案:

答案 0 :(得分:1)

您可以在CheckedChanged-Event

中执行此操作
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    // Do what you have to do...
}

答案 1 :(得分:1)

这是我的一些代码,您需要做的就是为您的datagridview创建一个“CellContentClick”事件。

最简单的方法是选择Datagridview,转到属性并单击闪电。向下滚动到“CellContentClick”并双击空框。这将自动生成将以下代码粘贴到其中所需的方法。

确保将我的“CustomersDataGridView”实例重命名为您的名字,intellisense应突出显示您需要替换的红色无效代码。

此外,您在checkCell声明中看到的“9”需要更改为“完成”复选框的索引。如果它在左边的第3个单元格中,则将2放在那里而不是9,因为索引是基于0的。

修改评论:

private void CustomersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

    if (e.ColumnIndex.ToString() == "9")
    {
        DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)CustomersDataGridView.Rows[e.RowIndex].Cells[9];
        DataGridViewRow row = CustomersDataGridView.Rows[e.RowIndex] as DataGridViewRow;

        if (Convert.ToBoolean(checkCell.EditedFormattedValue) && CustomersDataGridView.IsCurrentCellDirty)
        {
            //Do Work here.
            var z = row.Cells[0].Value; // Fill in the brackets with the column you want to fetch values from
            //z in this case would be the value of whatever was in the first cell in the row of the checkbox I clicked
        }
    }
}
相关问题