datagridview同列不同行动态嵌套不同类型控件

来源:互联网 发布:淘宝宝贝换主图有影响 编辑:程序博客网 时间:2024/05/01 09:39

当单击属性值列单元格时,会根据数据库中的标志位来判断显示textbox,还是combobox,或者其他控件。

当嵌套控件的值发生改变时,将值传给所在单元格。效果图如下:

实现过程如下:

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewCell dgvc = this.dataGridView1.CurrentCell;
            RnDevObject obj = devTreeView2._SelectedDevObject;

            if (dgvc != null && dgvc.OwningColumn.Name == "属性值")
            {

             //为0则显示textbox,1显示combobox
                         if (数据库标志位==0)
                        {
                            dataGridView1.Controls.Add(textBox1);
                            Rectangle R = dataGridView1.GetCellDisplayRectangle(dgvc.ColumnIndex, dgvc.RowIndex, false);
                            if (dgvc.Value != null)
                            {
                                textBox1.Text = dgvc.Value.ToString();
                            }
                            textBox1.Size = R.Size;
                            textBox1.Left = R.Left;
                            textBox1.Top = R.Top;
                            textBox1.Visible = true;
                            comboBox1.Visible = false;
                        }
                        else if (数据库标志位== 1)
                        {
                            dataGridView1.Controls.Add(comboBox1);
                            绑定combobox;
                            Rectangle R = dataGridView1.GetCellDisplayRectangle(dgvc.ColumnIndex, dgvc.RowIndex, false);
                            if (dgvc.Value != null)
                            {
                                comboBox1.Text = dgvc.Value.ToString();
                            }
                            comboBox1.Size = R.Size;
                            comboBox1.Left = R.Left;
                            comboBox1.Top = R.Top;
                            comboBox1.Visible = true;
                            textBox1.Visible = false;
                        }
                   
            }
            else
            {
                textBox1.Visible = false;
                comboBox1.Visible = false;
            }
        }



private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.SelectedText!=string.Empty)
            dataGridView1.CurrentCell.Value = comboBox1.Text;
        }
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            if(comboBox1.SelectedText!=string.Empty)
            dataGridView1.CurrentCell.Value = comboBox1.Text;

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell.Value = textBox1.Text.ToString();
        }

 

原创粉丝点击