2009-05-27 68 views
1

我有一个表单数据绑定到一个客户对象,其中一个字段是一个空的int表示一个“类型”。这显示为组合框,组合框绑定到“类型”表。如何将数据绑定对象的组合框字段绑定到数据源并仍然允许空值?

将具有空类型的客户加载到窗体的数据源中时,组合框不显示任何值,但是在单击它时必须选择一个值。表单/组合框永远不会让您变回空白项目(以在客户对象上表示“null”)。

我不想在数据库中使用“虚拟行”,目前通过添加一个虚拟对象并在提交事件(不干净!)中将其归零。

是否可以做到这一点干净,保持可空主键?

+0

也许你应该发布链接到上述问题(在SO上)或网络上的外部资源。 – Cerebrus 2009-05-27 17:18:44

+0

只是一个建议,如何使你的“类型”的枚举值为“未指定”?我知道这不是一个答案,但你应该使用枚举...... – BFree 2009-05-27 17:24:28

+2

在某些情况下,枚举可能是一个解决方案,但如果数据来自数据源,则不是。例如,考虑一款汽车的产品配置器 - 可能会有一个包含所有可用导航系统的组合框。在数据库中,表Car将有一个可空的外键给表NavigationSystems。因此,如果用户决定不购买导航系统,那么您需要将组合框绑定到NavigationSystems表,但仍然允许null。这可能是一个解决方案,添加一个新的行“没有导航系统”,并使外键不可为空 - 但这也有缺点。 – 2009-05-27 17:33:13

回答

-1

用于绑定到类型组合的数据源可能还有其他1个条目,其中包含NULL值。

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

您可以为没有类型的客户存储-1(如果允许客户不需要类型)。

0

关于为空的数据绑定附加链接和资源:

我一直在说,到目前为止,它已经摇了,你应该在你的数据库的数据源之间有业务对象层和你的用户界面,你可以只按照shahkalpesh的建议添加一个项目,而不用担心它会进入数据库。

Jez Humble在这个post and in comments的底部有一些关于绑定可空类型的信息如果“你显式地将DataSourceUpdateMode设置为DataSourceUpdateMode.OnPropertyChanged”,它建议绑定到可空类型是可行的。

在数据绑定可空类型的另一篇文章:代码的乐趣 - Databinding and Nullable types in WinForms.NET

也许这个代码结合可为空的DateTimePicker可以帮助你找到这个或其他可为空的问题addditional解决方案。

另外检查出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); 

    } 
0

如果您的主要目标是将组合恢复为空白且值为空,则只需在编辑行时按Ctrl + 0即可。

我花了两天的疯狂研究和心脏病发作的风险,只是为了找到隐藏在某个小帖子中的信息。