場景 滑鼠點擊DataGridView的某個單元格時,此單元格添加一個自定義的控制項,這裡以 添加下拉框為例 效果 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 在設計器頁 ...
場景
滑鼠點擊DataGridView的某個單元格時,此單元格添加一個自定義的控制項,這裡以
添加下拉框為例
效果
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
在設計器頁面,找到DataGridView的單元格點擊事件CellClick,然後雙擊進入其點擊事件中
private void dataGridView_Task_ViewEdit_CellClick(object sender, DataGridViewCellEventArgs e) { //獲取當前點擊的列的index int currentColumnindex = dataGridView_Task_ViewEdit.CurrentCell.ColumnIndex; //獲取當前行的index int currentRowindex = dataGridView_Task_ViewEdit.CurrentCell.RowIndex; switch (currentColumnindex) { case 2: //第三列-控制模式 Cell2Click(currentColumnindex,currentRowindex); break; case 3: //第四列-跳轉條件 break; case 4: //第五列-記錄條件 break; case 5: //第六列-電流量程 break; default: break; } }
然後在通過當前列的Index判斷是那一列,再執行具體的操作,添加不同的控制項。
這裡操作第三列,然後執行方法Cell2Click,並將當前行與列的index傳遞。
private void Cell2Click(int currentColumnindex, int currentRowindex) { //下拉框控制項 DevExpress.XtraEditors.ComboBoxEdit comboBox = new DevExpress.XtraEditors.ComboBoxEdit(); //添加ComboBox comboBox.Name = "ControlModel_ComBox"; ComboBoxItemCollection coll = comboBox.Properties.Items; //添加 this.dataGridView_Task_ViewEdit.Controls.Add(comboBox); //獲取當前單元格的內容 string currentCellValue = this.dataGridView_Task_ViewEdit.Rows[currentRowindex].Cells[currentColumnindex].Value.ToString(); //清空單元格內容 this.dataGridView_Task_ViewEdit.Rows[currentRowindex].Cells[currentColumnindex].Value = String.Empty; //獲取大小 Rectangle rect = dataGridView_Task_ViewEdit.GetCellDisplayRectangle(currentColumnindex, currentRowindex, true); //大小設置 comboBox.Size = new Size((rect.Width / 3), rect.Height); //位置設置 comboBox.Location = new Point(rect.Left, rect.Top); //根據配置文件獲取下拉框items選項 int i=0; List<ControlModelItem> controlModelItems = TaskViewEditHelper.GetComboBoxItems(System.IO.Path.Combine(Global.AppConfig.SysConfigPath, Global.CONTROL_MODEL_ITEMS_FILE_PATH)); foreach(ControlModelItem controlModelItem in controlModelItems) { coll.Add(controlModelItem); if (controlModelItem.Value == currentCellValue) comboBox.SelectedIndex = i; i++; } //通過下麵可以獲取選中項的內容 ////if (comboBox.SelectedItem != null) ////{ //// string key = (comboBox.SelectedItem as ControlModelItem).Key; //// string value = (comboBox.SelectedItem as ControlModelItem).Value; ////} //綁定事件--控制模式下拉框選項改變 comboBox.SelectedValueChanged += comboBox_SelectedValueChanged; }
這裡是添加了一個DevExpress的下拉框控制項ComboBoxEdit控制項,並添加下拉框選項,然後綁定下拉框內容改變的事件comboBox_SelectedValueChanged。
同理在改變下拉框選項的事件中在分別實現添加控制項
private void comboBox_SelectedValueChanged(object sender, EventArgs e) { int controlCount = this.dataGridView_Task_ViewEdit.Controls.Count; //初始化會有三個控制項 if (controlCount>3) { for (int i = 3; i < controlCount; i++) { //刪除第三個之後的控制項,刪除後索引減1 所以迴圈刪除第四個控制項 this.dataGridView_Task_ViewEdit.Controls.RemoveAt(3); } } DevExpress.XtraEditors.ComboBoxEdit comboBox = sender as ComboBoxEdit; ControlModelItem controlModelItem = comboBox.SelectedItem as ControlModelItem; string controlModelItemkey = controlModelItem.Key; switch (controlModelItemkey) { //恆壓 case "ConstantVoltage": int currentColumnindex = dataGridView_Task_ViewEdit.CurrentCell.ColumnIndex; int currentRowindex = dataGridView_Task_ViewEdit.CurrentCell.RowIndex; TextEdit textEdit = new TextEdit(); textEdit.Name = "ControlMode_ConstantVoltage_textEdit"; this.dataGridView_Task_ViewEdit.Controls.Add(textEdit); //獲取大小 Rectangle rect = dataGridView_Task_ViewEdit.GetCellDisplayRectangle(currentColumnindex, currentRowindex, true); //大小設置 textEdit.Size = new Size((rect.Width / 6) + Global.CONTROL_DISTANCE, rect.Height); //位置設置 textEdit.Location = new Point(rect.Left + (rect.Width / 3), rect.Top); LabelControl label = new LabelControl(); label.Name = "ControlMode_ConstantVoltage_label"; this.dataGridView_Task_ViewEdit.Controls.Add(label); label.Text = "V"; //位置設置 label.Location = new Point(rect.Left + (rect.Width / 3) + (rect.Width / 6) + Global.CONTROL_DISTANCE * 2, rect.Top + Global.LABEL_FROM_TOP_DISTANCE); break; case "Shelve": break; case "ConstantCurrent": break; case "ConstantPower": break; case "ConstantLoad": break; case "Cycle": break; case "CurrentSlope": break; case "CurrentLadder": break; case "ConstantVoltageLimitCurrent": break; case "CurrentPulse": break; case "WorkingConditionSimulation": break; case "PowerRamp": break; case "PowerLadder": break; default: break; } }