【问题标题】:C# - Add a default value to a column after the AddingNew of the dataBindingSourceC# - 在dataBindingSource的AddingNew之后为列添加默认值
【发布时间】:2018-01-20 18:38:05
【问题描述】:

当我在 dataBindingSource 中插入一个新行时,我想为一个字段分配一个默认值,这样如果用户没有输入任何值,这个值就会被默认分配。我不能在数据库中保留这个默认值,因为它可能会因场合而异。

那么,在用户点击插入按钮后,使用 dataBindingSource,如何将列(在插入的新行中)分配给默认值(预先确定)?我想过使用dataBindingSource的AddingNew事件。

我正在使用具有 bindingSource 和 tableAdapter 的数据集。我接受其他方法建议。

【问题讨论】:

  • 你使用过DataTable对象吗?
  • 不,我没有。我的表单只有以下数据控件:dataSet、tableAdapter 和 bindingSource。但我接受有关上述问题的任何建议。
  • 您也可以在数据集设计器中设置一个默认值。

标签: c# dataset strongly-typed-dataset


【解决方案1】:

我建议使用DataTable 对象,然后您可以轻松地为此类列设置默认值:

   DataTable dt = new DataTable();
   dt.Columns["Column1"].DefaultValue = "default";
  // dt.DataSet = ..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多