Ejemplo n.º 1
0
        /// <summary>
        /// 当焦点离开时,需要将新输入的值加入到组合框的 Items 列表中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void combo_Leave(object sender, EventArgs e)
        {
            ComboBox combo = sender as ComboBox;

            combo.Leave -= new EventHandler(combo_Leave);
            if (CurrentCell != null && CurrentCell.OwningColumn is DataGridViewComboBoxColumnEx)
            {
                DataGridViewComboBoxColumnEx col = CurrentCell.OwningColumn as DataGridViewComboBoxColumnEx;
                //一定要将新输入的值加入到组合框的值列表中
                //否则下一步给单元格赋值的时候会报错(因为值不在组合框的值列表中)
                col.Items.Add(combo.Text);
                CurrentCell.Value = combo.Text;
            }
        }
Ejemplo n.º 2
0
 protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
 {
     if (CurrentCell != null && CurrentCell.OwningColumn is DataGridViewComboBoxColumnEx)
     {
         DataGridViewComboBoxColumnEx col = CurrentCell.OwningColumn as DataGridViewComboBoxColumnEx;
         //修改组合框的样式
         if (col.DropDownStyle != ComboBoxStyle.DropDownList)
         {
             ComboBox combo = e.Control as ComboBox;
             combo.DropDownStyle = col.DropDownStyle;
             combo.Leave        += new EventHandler(combo_Leave);
         }
     }
     base.OnEditingControlShowing(e);
 }