C# winfrom Datagridview控件下拉菜单

时间:2022-02-19 14:45:45

拖拽一个datagridview放在界面,编辑列把下来菜单那列ColumnType设置成DataGridViewComboBoxColumn

然后在数据一栏的Items可以写下来菜单的内容也可以后台代码写

下面是后台代码实现功能

 private void dgUserAuthData_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgUserAuthData.CurrentCell.ColumnIndex == 5 && dgUserAuthData.CurrentCell.ColumnIndex != -1)
            {
                ComboBox combo = e.Control as ComboBox;
                combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
            }
        }

        public void combox_Leave(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            //做完处理,须撤销动态事件
            combox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ComboBox combo = (ComboBox)sender;
                combo.Leave += new EventHandler(combox_Leave);
                if (combo.SelectedIndex > -1)
                {
                    string selectedItem = combo.Text;//拿到选择后的值

                    DataGridViewRow dv = dgUserAuthData.CurrentRow;
                    MessageBoxButtons messButton = MessageBoxButtons.YesNo;
                    DialogResult dr = MessageBox.Show("确定要" + selectedItem + "该用户吗?", "确定" + selectedItem, messButton);
                    if (dr == DialogResult.No)
                    {
                        return;
                    }
                    if (userModel.MangerVoucher == "")
                    {
                        MessageBox.Show("您没有管理凭证修改不了");
                        return;
                    }
                    string[] colItems = { "UserPhoneNo", "AccessOperation" };
                    string[] valueItems = new string[2];//添加新增的内容
                    valueItems[0] = userModel.UserPhoneNo; valueItems[1] = selectedItem;
                    //判断有哪些权限,然后只能修改这些权限         
                    SQLiteDbHelper ScabHelper1 = new SQLiteDbHelper(TrhaConst.trhaSCAB);
                    if (ScabHelper1.dbConnection.State == ConnectionState.Open)
                    {
                        IDataReader sqReader = ScabHelper1.UpdateInto("CcUser", colItems, valueItems, "UserPhoneNo", userModel.UserPhoneNo);
                    }
                    if (ScabHelper1.dbConnection.State == ConnectionState.Open) ScabHelper1.CloseSqlConnection();
                    MessageBox.Show("修改成功");
                }
              
            }
            catch (Exception)
            {


            }
        }