C# DataGridView 中DataGridViewComboBoxCell 下拉列表选择事件

C# DataGridView 中DataGridViewComboBoxCell 下拉列表选择事件

问题:由于DataGridView控件没有SelectedIndexChanged选择事件,DataGridView需要加上EditingControlShowing事件进行转换,将需要数据用下拉框DataGridViewComboBoxCell显示,根据相应CurrentCell触发ComboBox_SelectedIndexChanged事件。
代码如下,根据当前需要进行代码的更换,有详细注释。

		/// <summary>
        /// 首先给这个DataGridView加上EditingControlShowing事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DataGridView_attribute_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //判断相应的列
            if (dataGridView_attribute.CurrentCell== L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1)
            {
                //给这个DataGridViewComboBoxCell加上下拉事件
                (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);

            }
            if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1)
            {
                //给这个DataGridViewComboBoxCell加上下拉事件
                (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);

            }
            if (dataGridView_attribute.CurrentCell == L0_Cell_2 && dataGridView_attribute.CurrentCell.RowIndex != -1)
            {
                //给这个DataGridViewComboBoxCell加上下拉事件
                (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);

            }
        }
       
        /// 组合框事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            //这里比较重要
            combox.Leave += new EventHandler(combox_Leave);
            try
            {
                string model_nums = sc_tabledata.number_of_holes.Trim();
                if (model_nums.Equals("单孔"))
                {
                    if (Convert.ToString(combox.SelectedItem) != sc_tabledata_1.Span_1)
                    {
                        sc_tabledata_1.Span_1 = Convert.ToString(combox.SelectedItem);
                    }
                }
                else if (model_nums.Equals("双孔"))
                {
                    if (dataGridView_attribute.CurrentCell == L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1)
                    {
                        L0_Cell.Value = combox.SelectedItem;
                        if (Convert.ToString(L0_Cell.Value) != sc_tabledata_1.Span_1)
                        {
                            sc_tabledata_1.Span_1 = Convert.ToString(L0_Cell.Value);
                        }
                    }
                    if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1)
                    {
                        L0_Cell_1.Value = combox.SelectedItem;
                        if (Convert.ToString(L0_Cell_1.Value) != sc_tabledata_1.Span_2)
                        {
                            sc_tabledata_1.Span_2 = Convert.ToString(L0_Cell_1.Value);
                        }
                    }

                }
                else if (model_nums.Equals("三孔"))
                {
                    if (dataGridView_attribute.CurrentCell == L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1)
                    {
                        L0_Cell.Value = combox.SelectedItem;
                        if (Convert.ToString(L0_Cell.Value) != sc_tabledata_1.Span_1)
                        {
                            sc_tabledata_1.Span_1 = Convert.ToString(L0_Cell.Value);
                        }
                    }
                    if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1)
                    {
                        L0_Cell_1.Value = combox.SelectedItem;
                        if (Convert.ToString(L0_Cell_1.Value) != sc_tabledata_1.Span_2)
                        {
                            sc_tabledata_1.Span_2 = Convert.ToString(L0_Cell_1.Value);
                        }
                    }
                    if (dataGridView_attribute.CurrentCell == L0_Cell_2 && dataGridView_attribute.CurrentCell.RowIndex != -1)
                    {
                        L0_Cell_2.Value = combox.SelectedItem;
                        if (Convert.ToString(L0_Cell_2.Value) != sc_tabledata_1.Span_3)
                        {
                            sc_tabledata_1.Span_3 = Convert.ToString(L0_Cell_2.Value);
                        }
                    }
                }
                //删全部行
                while (dataGridView_attribute.Rows.Count > 0)
                {
                    dataGridView_attribute.Rows.RemoveAt(0);
                }
                Sc_addRows();//重新建表行                    
                Sc_refreshTableCell();//刷新表格单元
                Sc_fill_dataview_data();//填数据
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
         //(这一步比较重要,如果不加,会导致selectedchanged事件一直触发)
        /// <summary>
        /// 离开combox时,把事件删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void combox_Leave(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            //做完处理,须撤销动态事件
            combox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
        }
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值