场景
鼠标点击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 :
//第三列-控制模式
Cell2Click(currentColumnindex,currentRowindex);
break;
case :
//第四列-跳转条件
break;
case :
//第五列-记录条件
break;
case :
//第六列-电流量程
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 / ), rect.Height);
//位置设置
comboBox.Location = new Point(rect.Left, rect.Top); //根据配置文件获取下拉框items选项
int i=;
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>)
{
for (int i = ; i < controlCount; i++)
{
//删除第三个之后的控件,删除后索引减1 所以循环删除第四个控件
this.dataGridView_Task_ViewEdit.Controls.RemoveAt();
}
}
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 / ) + Global.CONTROL_DISTANCE, rect.Height);
//位置设置
textEdit.Location = new Point(rect.Left + (rect.Width / ), 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 / ) + (rect.Width / ) + Global.CONTROL_DISTANCE * , 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;
}
}