日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DataGridView中实现点击单元格Cell动态添加自定义控件

發布時間:2025/3/19 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataGridView中实现点击单元格Cell动态添加自定义控件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

鼠標點擊DataGridView的某個單元格時,此單元格添加一個自定義的控件,這里以

添加下拉框為例

效果

?

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

在設計器頁面,找到DataGridView的單元格點擊事件CellClick,然后雙擊進入其點擊事件中

?

private void dataGridView_Task_ViewEdit_CellClick(object sender, DataGridViewCellEventArgs e){//獲取當前點擊的列的indexint currentColumnindex = dataGridView_Task_ViewEdit.CurrentCell.ColumnIndex;//獲取當前行的indexint 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();? //添加ComboBoxcomboBox.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;}}

?

總結

以上是生活随笔為你收集整理的DataGridView中实现点击单元格Cell动态添加自定义控件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。