代码之家  ›  专栏  ›  技术社区  ›  TheSoftwareJedi jac

如何将数据绑定对象的组合框字段绑定到数据源,同时仍然允许空值?

  •  1
  • TheSoftwareJedi jac  · 技术社区  · 17 年前

    我有一个绑定到客户对象的表单数据,其中一个字段是一个可以为null的int,表示“类型”。这显示为组合框,组合框绑定到“Types”表。

    我不希望数据库中出现“虚拟行”,目前是通过添加一个虚拟对象,并在提交事件中将其清零来实现的(不干净!)。

    是否有可能使用可为null的主键来干净地完成此操作?

    3 回复  |  直到 17 年前
        1
  •  0
  •   Maslow    16 年前

    post and in comments

    Databinding and Nullable types in WinForms.NET

    也许这段绑定可为null的DateTimePicker的代码可以帮助您找到解决此问题或其他可为null问题的其他解决方案。

    Dan Hannan 关于我提出扩展方法的来源。

    /// <summary>
        /// From BReusable
        /// </summary>
        /// <param name="dtp"></param>
        /// <param name="dataSource"></param>
        /// <param name="valueMember"></param>
        /// <remarks>With help from Dan Hanan at http://blogs.interknowlogy.com/danhanan/archive/2007/01/21/10847.aspx</remarks>
        public static void BindNullableValue(this DateTimePicker dateTimePicker, BindingSource dataSource, String valueMember,bool showCheckBox)
        {
            var binding = new Binding("Value", dataSource, valueMember, true);
    
            //OBJECT PROPERTY --> CONTROL VALUE
            binding.Format += new ConvertEventHandler((sender, e) =>
            {
                Binding b = sender as Binding;
    
                if (b != null)
                {
                    DateTimePicker dtp = (binding.Control as DateTimePicker);
                    if (dtp != null)
                    {
                        if (e.Value == null)
                        {
    
                            dtp.ShowCheckBox = showCheckBox;
                            dtp.Checked = false;
    
                            // have to set e.Value to SOMETHING, since it's coming in as NULL
                            // if i set to DateTime.Today, and that's DIFFERENT than the control's current
                            // value, then it triggers a CHANGE to the value, which CHECKS the box (not ok)
                            // the trick - set e.Value to whatever value the control currently has. 
                            // This does NOT cause a CHANGE, and the checkbox stays OFF.
    
                            e.Value = dtp.Value;
    
                        }
                        else
                        {
                            dtp.ShowCheckBox = showCheckBox;
                            dtp.Checked = true;
                            // leave e.Value unchanged - it's not null, so the DTP is fine with it.
                        }
    
                    }
    
                }
            });
            // CONTROL VALUE --> OBJECT PROPERTY
            binding.Parse += new ConvertEventHandler((sender, e) =>
            {
                // e.value is the formatted value coming from the control. 
                // we change it to be the value we want to stuff in the object.
                Binding b = sender as Binding;
    
                if (b != null)
                {
                    DateTimePicker dtp = (b.Control as DateTimePicker);
                    if (dtp != null)
                    {
                        if (dtp.Checked == false)
                        {
                            dtp.ShowCheckBox = showCheckBox;
                            dtp.Checked = false;
                            e.Value = (Nullable<DateTime>)null;
                        }
                        else
                        {
                            DateTime val = Convert.ToDateTime(e.Value);
                            e.Value = val;
                        }
                    }
                }
            });
            dateTimePicker.DataBindings.Add(binding);
    
        }
    
        2
  •  0
  •   ProfK    13 年前

    如果您的主要目标是将组合还原为空,将值还原为null,只需在编辑行时按Ctrl+0即可。

    我花了两天疯狂的研究和心脏病发作的风险,才发现这个隐藏在某个地方的小帖子里。

        3
  •  -1
  •   shahkalpesh    17 年前

    用于绑定到Type组合的数据源可以再有一个NULL值的条目。

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    TypeID   Name
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1        Business
    2        Government
    -1       NULL