欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-29 08:44:08
场景 鼠标点击DataGridView的某个单元格时,此单元格添加一个自定义的控件,这里以 添加下拉框为例 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 在设计器页 ......

场景

鼠标点击datagridview的某个单元格时,此单元格添加一个自定义的控件,这里以

添加下拉框为例

效果

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

 

 

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

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

在设计器页面,找到datagridview的单元格点击事件cellclick,然后双击进入其点击事件中

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

 

 

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

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;
            }
        }