The implementation of clicking Cell to dynamically add custom control in DataGridView

scene

When clicking a cell of DataGridView, a custom control is added to the cell. Here

Add a drop-down box as an example

Effect

 

 

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

On the designer page, find the cell click event cell click of DataGridView, and then double-click to enter its click event

 

 

private void dataGridView_Task_ViewEdit_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Get the index
            int currentColumnindex = dataGridView_Task_ViewEdit.CurrentCell.ColumnIndex;
            //Get the index
            int currentRowindex = dataGridView_Task_ViewEdit.CurrentCell.RowIndex;
            switch (currentColumnindex)
            {
                case 2:
                    //The third column-control model
                    Cell2Click(currentColumnindex,currentRowindex);
                    break;
                case 3:
                    //The fourth column-forward condition
                    break;
                case 4:
                    //The fifth column-Recording conditions
                    break;
                case 5:
                    //The sixth column-current range
                    break;
                default:
                    break;
            }
        }

 

Then determine which column is the current one through the Index of the current column, and then perform specific operations to add different controls.

Operate the third column here, then execute the method Cell2Click, and pass the index of the current row and column.

private void Cell2Click(int currentColumnindex, int currentRowindex)
        {
            //Dropdown control
            DevExpress.XtraEditors.ComboBoxEdit comboBox = new DevExpress.XtraEditors.ComboBoxEdit();  //Add to ComboBox
            comboBox.Name = "ControlModel_ComBox";
            ComboBoxItemCollection coll = comboBox.Properties.Items;
            //Add to
            this.dataGridView_Task_ViewEdit.Controls.Add(comboBox);
            //Get the contents of the current cell
            string currentCellValue = this.dataGridView_Task_ViewEdit.Rows[currentRowindex].Cells[currentColumnindex].Value.ToString();
            //Empty cell contents
            this.dataGridView_Task_ViewEdit.Rows[currentRowindex].Cells[currentColumnindex].Value = String.Empty;
            //Get size
            Rectangle rect = dataGridView_Task_ViewEdit.GetCellDisplayRectangle(currentColumnindex, currentRowindex, true);
            //Size setting
            comboBox.Size = new Size((rect.Width / 3), rect.Height);
            //Location setting
            comboBox.Location = new Point(rect.Left, rect.Top);
            
            //Get the drop-down box according to the configuration file items option
            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++;
            }
            //The content of the selected item can be obtained from the following
            ////if (comboBox.SelectedItem != null) 
            ////{
            ////    string key = (comboBox.SelectedItem as ControlModelItem).Key;
            ////    string value = (comboBox.SelectedItem as ControlModelItem).Value;
            ////}
            
            //Binding events--Control mode dropdown options change
            comboBox.SelectedValueChanged += comboBox_SelectedValueChanged;
        }

 

Here is to add a drop-down box control ComboBoxEdit control of devxpress, add a drop-down box option, and then bind the event combobox [selectedvaluechanged] that changes the content of the drop-down box.

Similarly, in the event of changing the drop-down box option, add controls in the respective implementation

private void comboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            int controlCount = this.dataGridView_Task_ViewEdit.Controls.Count;
            //Initialization will have three controls
            if (controlCount>3)
            {
                for (int i = 3; i < controlCount; i++)
                {
                    //Delete the control after the third, and the index will be reduced by 1 after the deletion, so the fourth control will be deleted circularly
                    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)
            {
                //Constant voltage
                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);
                    
                    //Get size
                    Rectangle rect = dataGridView_Task_ViewEdit.GetCellDisplayRectangle(currentColumnindex, currentRowindex, true);
                    //Size setting
                    textEdit.Size = new Size((rect.Width / 6) + Global.CONTROL_DISTANCE, rect.Height);
                    //Location setting
                    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";
                    //Location setting
                    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;
            }
        }

Tags: C# Programming

Posted on Sat, 02 Nov 2019 19:22:37 -0700 by rhunter007