Which method is agood practise of creating DataColumn? RRS feed

  • Question

  • Hi Friend,

    There are several way of  creating and addinf columns to a table:

    i typically use two techniques:

    1. I create a columb object using datacolumn class then i add that to a columns collection

    DataColumn dcCol1 = new DataColumn("Col1", typeof(int));


    2.  use the Add() of columns collection of the table to add a Column:

    DataTable dt1 = new DataTable("Tale-1");
    dt1.Columns.Add("col1", typeof(int));

    which method is better in creating data columns?


    Saturday, January 2, 2010 4:51 AM


  • In the first approach, you can define the behaviours of the DataColumn such as AllowDBNull, AutoIncrement, DefaultValue, MaxLength etc before adding to the Columns collection of DataTable. But in the second approach you cannot specify the above properties while adding a column to the Columns collection of DataTable. You can do it only after getting the reference to the column but before adding data to the datatable. The better approach is based on your requirement.
    • Marked as answer by code_warrior Saturday, January 2, 2010 5:58 AM
    Saturday, January 2, 2010 5:42 AM