1. 程式人生 > >C# winfrom Datagridview控制元件下拉選單

C# winfrom Datagridview控制元件下拉選單

拖拽一個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) { } }