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

时间:2014-05-19 18:45:02   收藏:0   阅读:954

以下是参考代码

bubuko.com,布布扣
        //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;
                }
            }
        }
bubuko.com,布布扣

 

 

 

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!