• 关于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:查询语句中字段名表名建议一律加[] 否则 字段中出现空格 特殊字符(比如“-”) 报错现象

  • 相关阅读:
    selenium窗口切换
    方案:餐厅饭店AI智能视频监控可视化监管系统搭建方案
    手撕无头单链表
    Java8 Stream生成流 generate iterate IntStream
    存储系统基本概念
    5-2:Kafka入门
    HarmonyOS开发(一):开发工具起步
    Postgresql 阿里云部署排雷
    【MyBatis-Plus】快速精通Mybatis-plus框架—核心功能
    nginx设置缓存proxy_cache
  • 原文地址:https://blog.csdn.net/sapch33/article/details/133258752