场景

鼠标点击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;
}
}

DataGridView中实现点击单元格Cell动态添加自定义控件的更多相关文章

  1. C# 在DataGridView中,点击单元格调出 TreeView控件 或 ListBox控件

    1.调出 TreeView控件 或  ListBox控件 private void deductGrid1_CellClick(object sender, DataGridViewCellEvent ...

  2. Easyui之datagrid实现点击单元格修改单元格背景颜色

    前段时间有个需求中有点击datagrid的单元格实现某种事件,调用datagrid的onclickCell这个方法很容易实现,但是体验不好啊,完全不知道自己刚才点击的是哪个单元格,然后就尝试单击单元格 ...

  3. dev gridview指定单元格cell获取坐标

    DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo Info2 = gvQueryResult.GetViewInfo() as DevExpre ...

  4. DataGridView获取或者设置当前单元格的内容

    当前单元格指的是DataGridView焦点所在的单元格,它可以通过DataGridView对象的CurrentCell属性取得.如果当前单元格不存在的时候,返回null. 取得当前单元格的内容: o ...

  5. GridView控件点击单元格如何获取该列的列标题

    本博文Insus.NET教你实现在GridView控件中,用mouse点击某单元格之后,希望能获取到该列的列标题. 创建一个网页,创建一个GridView控件: 去cs绑定数据给GridView控件: ...

  6. jxl获取excel中的合并的单元格(主要是方法介绍)

    Range[] rangeCells = sheet.getMergedCells();// 返回sheet中合并的单元格数组 for (Range r : rangeCells) {//对数组遍历拿 ...

  7. Office EXCEL 中如何让一个单元格的数据链接到另一个工作表的数据

    比如我在Sheet2中定义了几个数据,这些都是简单的数字,而在Sheet1中让要被绑定的单元格等于Sheet2的对应单元格地址(比如Sheet2!B1,Sheet2!B2之类的)   然后就可以一改全 ...

  8. EXCEL中对1个单元格中多个数字求和

    如A1=3779.3759.3769.3781.3750,A2对A1中4个数字求和怎么求!请高手赐教! 方法一:在B1中输入公式=SUM(MID(A1,{1,6,11,16,21},4)*1) 方法二 ...

  9. react ,ant Design UI中table组件合并单元格并展开详情的问题

    需求:购物车订单列表,如图: 一:单元格合并 遇到这种你会怎么办呢?  单元格合并?  还是其他的方法? 下面是我的处理方式,就是在table 组件的columns上处理,这里拿商品举例,其余的类似, ...

随机推荐

  1. 使用IntelliJ IDEA创建第一个Mawen项目

    咳咳...首先各位在学习SSM框架的时候,单个单个学完之后,老夫掐指一算(其实,我是个小鲜肉),各位必定会去整合SSM,这个时候,老夫又掐指一算,各位必定会碰到个mawen的东西,在这里,我可以告诉各 ...

  2. 获取windows操作系统所有用户

    一.知识点简单介绍 1. 利用WindowsApi获取 [DllImport("Netapi32.dll ")] extern static int NetUserEnum([Ma ...

  3. react使用moment进行日期格式化

    导入moment import moment from 'moment'; 使用方式 年月日,时分秒 moment().format('YYYY-MM-DD HH:mm:ss'); // 2019-0 ...

  4. 解决NuGet下载太慢的问题

    以下载CefSharp.Wpf v57.0.0版本为例: 1.打开NuGet官网:https://www.nuget.org/ 2.输入CefSharp.Wpf,点击查询,如下所示: 3.确认版本正确 ...

  5. Can not find the tag library descriptor for “http://java.sun.com/jstl/core"

    此文原博文地址:https://blog.csdn.net/kolamemo/article/details/51407467 按照查到的资料,JSTL taglib需要jstl.jar来支持.在1. ...

  6. mapreduce shortest way out

    相关知识 最优路径算法是无向图中满足通路上所有顶点(除起点.终点外)各异,所有边也各异的通路.应用在公路运输中,可以提供起点和终点之间的最短路径,节省运输成本.可以大大提高交通运输效率. 本实验采用D ...

  7. 松软科技web课堂:SQLServer之UCASE() 函数

    UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name SQL UCASE() ...

  8. SAP系统邮件功能配置

    相信SAP顾问或多或少都会接到用户要求SAP系统能够定时发送邮件的功能,定时将用户需要的信息已邮件的方式发送给固定的人员. 下面就来讲一下SAP发送邮件应该如何配置: 1.RZ10做配置: is/SM ...

  9. iOS---------开发中 weak和assign的区别

    weak和assign的区别-正确使用weak.assign 一.区别 1.修饰变量类型的区别weak只可以修饰对象.如果修饰基本数据类型,编译器会报错-“Property with ‘weak’ a ...

  10. MySQL基础之常用函数

    数学函数的使用 常用数学函数 函数 作用 函数 作用 ceil() 进一取整 abs() 取绝对值 floor() 舍掉小数部分 power() 幂运算 round() 四舍五入 pi() 圆周率 t ...