【问题标题】:Only check checkbox if popup = yes仅在 popup = yes 时选中复选框
【发布时间】:2013-08-02 23:40:07
【问题描述】:

我在datagridview 中有一个checkbox,我需要一个是/否弹出窗口来确认SQL 端的事务。这部分工作正常,但如果我选择否,复选框仍会选中。

如何确保复选框在 UI 上具有正确的值?

我使用的代码在cellcontentclick 中,正确复选框的代码如下:

if (e.ColumnIndex.ToString() == "3")
{
    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)caseSelectorDataGridView.Rows[e.RowIndex].Cells[3];

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

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

    SelectedRowView = (System.Data.DataRowView)caseSelectorBindingSource.Current;
    SelectedRow = (newCityCollectionDataSet.CaseSelectorRow)SelectedRowView.Row;

    DialogResult dialogResult = MessageBox.Show("Are you sure?", "WARNING", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes && Convert.ToBoolean(checkCell.Value) == false) 
    {
        if (Convert.ToBoolean(checkCell.Value) == false && caseSelectorDataGridView.IsCurrentCellDirty)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

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

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

            DateTime today = DateTime.Now;
            DateTime firstDay = new DateTime(today.Year, today.Month, 1);
            reportsSent newReport = new reportsSent();
            newReport.CaseNumberKey = SelectedRow.CaseNumberKey;
            dc.reportsSents.InsertOnSubmit(newReport);
            matchedCaseNumberProp.DateFinished = DateTime.Now;
            matchedCaseNumberProp.Finished = -1;

            dc.SubmitChanges();

            SelectedRow.FinishedDate = DateTime.Now;
            SelectedRow.FinishedID = Globals.GlobalInt;
            SelectedRow.Finished = -1;

            caseSelectorTableAdapter.Update(newCityCollectionDataSet);

            var qry = dc.PropertyInformations.Where(c => c.DateFinished >= firstDay && c.DateFinished <= today && c.ClientKey == 2).Count();
            var qry1 = dc.PropertyInformations.Where(c => c.DateFinished >= firstDay && c.DateFinished <= today && c.ClientKey == 3).Count();

            txtFinished.Text = Convert.ToString(qry);
            txtGRBFinished.Text = Convert.ToString(qry1);
            txtFinished.Text = Convert.ToString(qry);

            PerformRefresh();
        }
    }
    else if (dialogResult == DialogResult.No)
    {

    }
}

【问题讨论】:

  • 我在任何地方都看不到您将检查状态设置为真或假...您调用了您未向我们展示的可能包含错误的函数。如果您需要有用的答案,您还应该删除不相关的代码并评论您的代码。
  • 在 c# winform datagridview 复选框中单击时,默认设置选中状态。我不知道在哪里可以访问它,所以我可以确保它只在我想要它的时候出现,因此是这个问题。我所做的就是将值 -1 传递给数据库。
  • 您可以在上面的代码中看到您正在从checkCell.Value 访问它。
  • 当然,但是如果我删除该代码,它仍然会选中该框。
  • 我不是说代码在设置它,我是说你可以看到从哪里访问它。看来你已经解决了你的问题。

标签: c# winforms checkbox


【解决方案1】:

假设我没看错,试试:

else if (dialogResult == DialogResult.No)
{
    checkCell.Value = checkCell.FalseValue;
}

【讨论】:

  • 不幸的是,这并没有完成工作。它仍然选中该框。谢谢你,虽然我不知道 .FalseValue!
  • PerformRefresh 只是我写的一个刷新 DGV 的方法。
【解决方案2】:
DialogResult dialogResult = MessageBox.Show("Are you sure?", "WARNING", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No) return;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2021-09-22
    相关资源
    最近更新 更多