• 关于DatagridviewComboBox控件的若干技术问题


    一:DatagridviewComboBox 选定索引更改时更改 DatagridviewTextBox 文本内容

     private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
            {
                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
            }
        }
    
        private void LastColumnComboSelectionChanged(object sender, EventArgs e)
        {
            var currentcell = dataGridView1.CurrentCellAddress;
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
            cel.Value = sendingCB.EditingControlFormattedValue.ToString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    二。添加DatagridviewComboBox下拉组合框内容的二种方式

    //方式一
    ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("A" );
    ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("B");
    ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("C" );
    
    //方式二
     List<string> ListData = new List<string> { "A", "B", "C" };
     ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).DataSource = ListData;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三。组合框内容的显示

    //1要设置显示类型 2要设置的值为组合列表内的元素
     this.Section.ValueType = typeof(string);
     this.Section_data.Rows[index1].Cells[2].Value = "B";
    
    • 1
    • 2
    • 3

    四。数据库小技巧

    1:DataGridViewComboBoxCell 有3个值:null\true\false
    2:字典作为数据源的绑定方法

    ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[4]).DataSource = new BindingSource(_typeDic, null);                ((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[4]).ValueMember = "Key";//文本对应的值
    
    • 1

    3:ComboBox组件的数据绑定,三个属性是:“DisplayMember”、 “ValueMember”、“DataSource”。其中
    "DataSource"是要显示的数据集
    DisplayMember绑定的是需显示的字段 给用户看
    ValueMember绑定的是对应的值 给程序员用
    4:查询语句中字段名表名建议一律加[] 否则 字段中出现空格 特殊字符(比如“-”) 报错现象

  • 相关阅读:
    Linux文件/目录高级管理二
    华为云云耀云服务器L实例评测 | 云服务器搭建自己的gitlab代码仓库手把手教学
    liunx安装docker
    I2C知识笔记
    LocalDate、LocalTime、LocalDateTime常用方法
    【自动驾驶】使用同心区域模型改进地面点云快速分割算法
    Win11电脑一段时间不操作就断网怎么解决
    elasticsearch的安装及使用
    探索珞石机器人|在汽车零部件检测上的应用
    CSS宽度100%和宽度100vw之间有什么不同?
  • 原文地址:https://blog.csdn.net/sapch33/article/details/133258752