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

发布于 2024-12-11 04:16:41 字数 1479 浏览 0 评论 0原文

我有一个带有多个复选框的 datagridview。选中“完成”复选框后,我需要执行 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();

        }
    }

我是否需要在某个时候结束编辑,这是问题所在吗?

I have a datagridview with multiple checkboxes. When the Finished checkbox is checked I need to execute linq code to update a specific table. How do I find out if that specific check box is dirty and where do I write the code to pass the values I need to be passed to the table. Note that it is not the same table that the datagridview is based on.

Thanks.

EDIT:

 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();

        }
    }

Do I need to endedit at some point is that the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-12-18 04:16:41

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

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

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

此外,您在 checkCell 声明中看到的“9”需要更改为“Finished”复选框的索引。如果它位于左侧第三个单元格中,则在此处输入 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
        }
    }
}

This is from some of my code, all you need to do is create a "CellContentClick" event for your datagridview.

The easiest way to do this is select the Datagridview, go to properties and click on the lightning bolt. Scroll down to "CellContentClick" and double click in the empty box. This will auto generate the method you need to paste the following code into.

Make sure you rename my instances of "CustomersDataGridView" to whatever yours is named as well, intellisense should highlight invalid code in red that you need to replace.

Also, the "9" you see in the checkCell declaration needs to be changed to the index of your "Finished" check-box. If it is in the 3rd cell from the left, put a 2 there instead of a 9, as the indexing is 0 based.

EDITTED to fix comments:

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
        }
    }
}
心意如水 2024-12-18 04:16:41

您可以在 CheckedChanged-Event 中执行此操作

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    // Do what you have to do...
}

You can do it in the CheckedChanged-Event

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    // Do what you have to do...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文