Search code examples
c#datasetstrongly-typed-dataset

C# - Add a default value to a column after the AddingNew of the dataBindingSource


When I insert a new row in the dataBindingSource I would like to assign a default value to a field so that if the user did not enter any value this value would be assigned by default. I can not leave this default value in the database because it may vary depending on the occasion.

So, after the user hit the insert button, using a dataBindingSource, how can I assign a column (in the new row being inserted) to a default value (pre determined)? I thought about using the AddingNew event of the dataBindingSource.

I am using a dataSet with bindingSource and tableAdapter. I accept other suggestions for approach.


Solution

  • I suggest to use DataTable object, then you can easily set a default value for such column :

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