首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridViewCheckBoxCell单击处理问题

DataGridViewCheckBoxCell单击处理问题
EN

Stack Overflow用户
提问于 2013-12-26 20:06:45
回答 1查看 7.9K关注 0票数 4

我创建了一个DataGridView控件,其中第一列是一个复选框。当我单击“在复选框中”时,代码将正确执行。但是,当单击发生在单元格中但不在复选框中时,代码将正确处理复选框的状态,但不会更新复选框本身,因此它仍处于单击之前的状态。

我该怎么纠正呢?

代码语言:javascript
复制
   private void myDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
   {
       if (e.RowIndex == -1) return; //check if row index is not selected
       DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
       ch1 = (DataGridViewCheckBoxCell)myDataGrid.Rows[e.RowIndex].Cells[0];

       if (ch1.Value == null)
           ch1.Value = false;
       switch (ch1.Value.ToString())
       {
           case "False":

               ch1.Value = true;

               break;
           case "True":

               ch1.Value = false;
               break;
       }
      //BUT doesn't seem to matter what I do with ch1.Value. Inside the checkbox all is OK but
      //outside, no.

   }

如果我单击该行上的另一个单元格,则将正确处理复选框。只有当我单击复选框单元格,而不是在复选框本身。

我的意思是(很抱歉),代码正确执行,但UI没有正确更新以反映更改。因此,如果复选框在未选中之前未选中,则在其后显示未选中。

病房也是,尽管它实际上是被检查的。

谢谢罗恩

EN

回答 1

Stack Overflow用户

发布于 2015-08-19 02:28:53

我百分之百肯定我做得很好。

代码语言:javascript
复制
 /// <summary>
    ///データグリッドビューのチェックボックスのダブルクリックイベント
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Ichiran_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.Ichiran_CheckBoxCellClick(sender, e);
    }

    /// <summary>
    /// データグリッドビューのチェックボックスのクリックイベント
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Ichiran_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        this.Ichiran_CheckBoxCellClick(sender, e);
    }

    /// <summary>
    /// セルの値自体がクリックされた際に発生するCellContentClickイベントの
    /// コールバックメソッドです。
    /// </summary>
    /// <param name="sender">イベント送信元オブジェクト</param>
    /// <param name="e"></param>              
    private void Ichiran_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        this.Ichiran_CheckBoxCellClick(sender, e);
    }

    /// <summary>
    /// チェックボックスクリック
    /// </summary>
    private void Ichiran_CheckBoxCellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (this.Ichiran.CurrentCell != null && this.Ichiran.CurrentCell is r_framework.CustomControl.DataGridCustomControl.DgvCustomCheckBoxCell)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            var cell = this.Ichiran["chb_delete", e.RowIndex];
            if (cell.Value == null) cell.Value = false;
            cell.Value = !(bool)cell.Value;
            cell.ReadOnly = true;
            cell.ReadOnly = false;
        }
    }

    /// <summary>
    /// CurrentCellDirtyStateChangedイベントハンドラでDataGridView.CommitEditメソッドを呼び出して値をコミットします。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Ichiran_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        this.Ichiran.CommitEdit(DataGridViewDataErrorContexts.Commit);

    }
票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20790640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档