删除文本时,Combobox不会恢复为NULL。 [英] Combobox does not revert to NULL when text is deleted.

查看:67
本文介绍了删除文本时,Combobox不会恢复为NULL。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定它是否是.NET中的缺陷,但是:

Not sure whether it is a flaw in .NET or not, but:

 如果用户清空了一个组合框(即想要删除该值)所选值不会恢复为null,而是保留最后一个有效的选定值,因此当您使用空白组合框进行保存时,它将恢复为原始值。

 If a user empties a combo box (i.e. wants to blank out the value) the selected value does not revert to null, Instead it keeps the last valid selected value, so when you save with a blank combobox it goes back to the original value.

一个解决方法是确保Value为NULL 首先从下拉列表中选择一个选项,单击"保存",然后关闭。再次打开它,删除它然后保存。然后它将使用空值保存到数据库中。 

One workaround to ensure that Value is NULL  is to first choose an option from the dropdown click save, and close. Open it again, delete it and then save. Then it will save into the database with the null value. 

建议的解决方案:

我可以以某种方式解决这个问题,不确定(因此在这里寻求帮助),并以一种方式使用,当使用清空组合框中的文本时(注意:Combobx不是下拉列表,而只是一个下拉列表)并保存,它将看到空白文本,并将值
保存在数据库中为null。 

I can either fix this somehow, not sure (hence asking for help here) and have it in a way that when use empties the text in the combobox (NOTE: Combobx is not a dropdown list rather just a dropwdown) and saves, it will see the blank text and save the value in the database as null. 

其他选项是我可以将它作为下拉列表以BLANK / NULL作为选项所以单击它时会将NULL保存为值。

Other option is that I can have it as a dropdown list with the BLANK /NULL as option so that when it is clicked it will save NULL as value.

我是新手,因此需要帮助。

I am new to this, hence will need help.

它确实使用数据绑定,因此不确定如何添加空值,我也不想将null选项添加到该表,因为它包含人员的名称和用户名。因此,如果没有选择用户,则应保存为NULL。

It does use databinding, hence not sure how to add the null value, also I do not want to add the null option to that table as it contains names and usernames for people. So if there is no user selected it should save as NULL.

此代码的位和 件如下:

Bits and pieces of this code is the following:

    private void InitializeComponent()        {           

this.components = new System.ComponentModel.Container();           

this.components = new System.ComponentModel.Container();           

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DealsRibbonForm));           

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DealsRibbonForm));           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();           

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();           

this.pnlCSRs = new System.Windows.Forms.Panel();            this.cmbSecCSR = new System.Windows.Forms.ComboBox();           

this.pnlCSRs = new System.Windows.Forms.Panel();            this.cmbSecCSR = new System.Windows.Forms.ComboBox();           

this.csrBindingSource2 = new System.Windows.Forms.BindingSource(this.components);           

this.csrBindingSource2 = new System.Windows.Forms.BindingSource(this.components);           

this.cmbCameraRC = new System.Windows.Forms.ComboBox();           

this.cmbCameraRC = new System.Windows.Forms.ComboBox();           

this.csrBindingSource3 = new System.Windows.Forms.BindingSource(this.components);  

this.csrBindingSource3 = new System.Windows.Forms.BindingSource(this.components);  

// pnlCSRs           

// pnlCSRs           

//            

//            

this.pnlCSRs.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;           

this.pnlCSRs.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;           

this.pnlCSRs.Controls.Add(this.cmbSecCSR);

// cmbSecCSR
//
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember =" Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112,26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.Name =" cmbSecCSR" ;;
this.cmbSecCSR.Size = new System.Drawing.Size(184,21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember =" Username" ;;
this.cmbSecCSR.TextChanged + = new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter + = new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember =" CSR" ;;
this.csrBindingSource2.DataSource = this.productionDS;

this.pnlCSRs.Controls.Add(this.cmbSecCSR); // cmbSecCSR // this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; this.cmbSecCSR.DataSource = this.csrBindingSource2; this.cmbSecCSR.DisplayMember = "Name"; this.cmbSecCSR.FormattingEnabled = true; this.cmbSecCSR.Location = new System.Drawing.Point(112, 26); this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0); this.cmbSecCSR.Name = "cmbSecCSR"; this.cmbSecCSR.Size = new System.Drawing.Size(184, 21); this.cmbSecCSR.TabIndex = 2; this.cmbSecCSR.ValueMember = "Username"; this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged); this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered); // // csrBindingSource2 // this.csrBindingSource2.DataMember = "CSR"; this.csrBindingSource2.DataSource = this.productionDS;

与cmbSecCSR关联的两个事件句柄如下: 

The two Event Handles associated with cmbSecCSR are the following: 

    private void comboBox_TextChanged(object sender, EventArgs e)
        {
            ComboBox cmbx = (ComboBox)sender;
            if (cmbx.Equals(cmbCamSupplier))
            {

            }
            else if (cmbx.Equals(cmbLGSupplier))
            {

            }
            if (cmbx.Text.Length > 0) return;
            cmbx.SelectedValue = "";
            cmbx.SelectedIndex = -1;
        }


  private void cmbBox_Entered(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;
            String txt = cmb.Text;
            if (cmb.Name.Contains("CSR"))
            {
                if (cmb != null)
                {
                    ((BindingSource)cmb.DataSource).Filter = (cmbOffice.SelectedIndex > -1 ? "Office = '" + cmbOffice.SelectedValue + "' AND " : "") + "IsCSR=1 AND Status=1";
                    cmb.Text = txt;
                }
            }
            else if (cmb.Name.Contains("RC"))
            {
                int department = 0;
                if (cmb != null)
                {
                    if (cmb.Name.Contains("Camera"))
                        department = 2;
                    else if (cmb.Name.Contains("LG"))
                        department = 3;
                    else if (cmb.Name.Contains("Power"))
                        department = 4;
                    ((BindingSource)cmb.DataSource).Filter = (cmbOffice.SelectedIndex > -1 ? "Office = '" + cmbOffice.SelectedValue + "' AND " : "") + "IsCSR=0 AND Status=1 AND (Department = " + department + " OR Department is null OR Department = 0)";
                    cmb.Text = txt;
                }
            }
        }

请帮助,我一直在努力解决这个问题。 

Please HELP, I have been struggling with this for a while now. 

推荐答案

我看不到ComboBox的ItemsSource的详细信息,但只要你有'null'的话在那里,你应该能够选择它。就目前而言,ComboBox无法选择不在ItemsSource中的项目。
I can't see the details of your ItemsSource for the ComboBox, but as long as you have 'null' in there, you should be able to select it. As it stands, the ComboBox cannot select an item that's not in the ItemsSource.


这篇关于删除文本时,Combobox不会恢复为NULL。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆