首页 文章

更改当前单元格后,将撤消C#datagridview单元格值编辑

提问于
浏览
1

我希望你能帮助我 . 我没有通过谷歌搜索找到适合我的问题的解决方案 .

我有一个没有绑定数据的DataGridView . 当用户双击单元格时,他可以将新值写入单元格 . 基于该变化,邻居小区中的值也被改变 . 到目前为止,这是正常的 .

现在我想设置一些条件,如阈值,如果用户超出范围,则应更正已编辑单元格中的值 . 相邻单元格中的值仍然是正确计算的,但用户编辑的单元格中的值始终保持在用户输入值 .

我已经尝试过BeginEdit()和CurrentCell的几种组合......但总是一样的 .

这是我的cellvalueChanged事件的实际代码:

private void dgSpotInfo_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (cellvaluechangend)
        {
            cellvaluechangend = false;

            int mod_Int_index = dgSpotInfo.Columns.IndexOf(dgSpotInfo.Columns["col_modifiedIntensity"]);

            dgSpotInfo.EditMode = DataGridViewEditMode.EditProgrammatically;
            dgSpotInfo.BeginEdit(true);
            int val;

            if (e.ColumnIndex == mod_Int_index) // if the modified Intensity value is changed
            {
                val = (Convert.ToInt32(dgSpotInfo[mod_Int_index, e.RowIndex].Value) - Convert.ToInt32(spot_analysis.GetFoundSpots[e.RowIndex].ModifiedIntensity));

                // here is tested if the value is too high

                if (Convert.ToInt32(dgSpotInfo[mod_Int_index, e.RowIndex].Value) > 255)
                {
                    // here is the problem, during debugging the value is displayed right, 
                    // but afterwards the current cell it is displayed as the too high value again

                    dgSpotInfo[mod_Int_index, e.RowIndex].Value = "255";
                    val = (Convert.ToInt32(dgSpotInfo[mod_Int_index, e.RowIndex].Value) - Convert.ToInt32(spot_analysis.GetFoundSpots[e.RowIndex].ModifiedIntensity));
                }

                dgSpotInfo.CurrentCell = dgSpotInfo[modifier_index, e.RowIndex];
                dgSpotInfo[modifier_index, e.RowIndex].Value = Convert.ToString(val);
            }

            dgSpotInfo.EndEdit();
            dgSpotInfo.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;

            dgSpotInfo.ReadOnly = true;
        }
    }

在这里点击我的点击事件:

private void dgSpotInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        // Testing if the clicked cell is allowed to change
        if (((e.ColumnIndex == dgSpotInfo.Columns.IndexOf(dgSpotInfo.Columns["col_modifiedIntensity"])) || (e.ColumnIndex == dgSpotInfo.Columns.IndexOf(dgSpotInfo.Columns["col_Modifier"]))) && e.RowIndex >= 0)
        {
            dgSpotInfo.CurrentCell = dgSpotInfo[e.ColumnIndex, e.RowIndex];
            dgSpotInfo.ReadOnly = false;
            dgSpotInfo.BeginEdit(true);
            cellvaluechangend = true;
        } 
    }

我希望我说出我的问题可以理解,你可以帮助我 . 坦克!

1 回答

  • 0

    如果有人有类似的问题:我通过添加CellEndEdit事件解决了这个问题 . 在用户编辑单元格之后,我正在CellEndEdit事件处理程序中执行上述阈值验证 . 我不知道为什么,但也许是这样,在由编辑引起的事件处理程序中,您无法撤消编辑 .

相关问题