C# winform datagridview 内嵌控件值改变后立即触发事件,而不需要离开该单元格时才触发,此时需要用到dgv_CurrentCellDirtyStateChanged事件

时间:2021-12-25 20:55:27

以下是参考代码

        //datagridview内嵌控件值修改事件
        private void dgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgv.IsCurrentCellDirty)
            {
                dgv.CurrentCellDirtyStateChanged -= dgv_CurrentCellDirtyStateChanged;
                dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
                dgv.CurrentCellDirtyStateChanged += dgv_CurrentCellDirtyStateChanged;
                c = dgv.CurrentCellAddress.X;
                r = dgv.CurrentCellAddress.Y;
                DataGridViewComboBoxCell cbbCellContorlType = (DataGridViewComboBoxCell)dgv.Rows[r].Cells["innerContorlType"];
                DataGridViewComboBoxCell cbbCellDefaultValue = (DataGridViewComboBoxCell)dgv.Rows[r].Cells["defaulValue"];
                DataGridViewCheckBoxCell cbCell = (DataGridViewCheckBoxCell)dgv.Rows[r].Cells["isEdit"];
                switch (dgv.Columns[c].Name)
                {
                    case "isEdit":
                        #region 修改是否可编辑
                        if (!bool.Parse(cbCell.EditedFormattedValue.ToString()))
                        {
                            cbbCellContorlType.Value = "";
                            cbbCellContorlType.ReadOnly = true;
                            cbbCellDefaultValue.Value = "";
                            cbbCellDefaultValue.ReadOnly = true;
                        }
                        else
                        {
                            cbbCellContorlType.ReadOnly = false;
                            cbbCellContorlType.Value = "TextBox";
                            cbbCellDefaultValue.ReadOnly = false;
                            cbbCellDefaultValue.Value = "";
                        }
                        #endregion 修改是否可编辑
                        break;
                    case "defaulValue":
                        #region 修改控件类型
                        string ctrlType = cbbCellContorlType.Value.ToString();
                        string ctrlItem = cbbCellDefaultValue.Value.ToString();
                        if (ctrlItem == "增加值…")
                        {
                            switch (ctrlType)
                            {
                                case "DropDownList":
                                    #region DropDownList
                                    frmddl.Show();
                                    frmddl.tbDDL.Clear();
                                    foreach (string item in cbbCellDefaultValue.Items)
                                    {
                                        int n = cbbCellDefaultValue.Items.IndexOf(item);
                                        if (n > 1)
                                        {
                                            if (n == 2)
                                                frmddl.tbDDL.Text += item;
                                            else
                                                frmddl.tbDDL.Text += "\r\n" + item;
                                        }
                                    }
                                    frmddl.btnConfirm.Click += btnConfirm_Click;
                                    #endregion DropDownList
                                    break;
                                case "ImageButton":
                                    #region ImageButton
                                    frmbtn.Show();
                                    frmbtn.btnConfirmImage.Click += btnConfirmImage_Click;
                                    #endregion ImageButton
                                    break;
                            }
                        }
                        #endregion 修改控件类型
                        break;
                    default:
                        break;
                }
            }
        }