Search code examples
datatablesqldatatypes

DataType of a Column in DataTable


I am creatin a datatable in C#,(sharepoint 2010). I want to add a column of datatype date having format "dd/mm/yyyy"

Can anyone tell me how to define a datatype for this column?

I tried this:

//SharePoint2010
    table.Columns.Add("Start Date", typeof(DateTime));

    To display in dd/MM/yyyy
    DateTime sdt = DateTime.Parse(req["Start_x0020_Date"].ToString());

    row["Start Date"] = sdt.ToString("dd/MM/yyyy");

But i am getting the output as dd/MM/yyyy HH:MM:SS

Please help i want the output as dd/MMyyyy


Solution

  • Add DateTime column to the DataTable and later you may format it to "dd/MM/yyyy" format.

    DataTable dataTable=new DataTable();
    dataTable.Columns.Add("date",typeof(DateTime));
    

    EDIT: No need to use DateTime column type to store string. Use string column type.

     table.Columns.Add("Start Date");
     ..
     table.Rows.Add(sdt.ToString("dd/MM/yyyy"));
     //OR
     row["Start Date"] = sdt.ToString("dd/MM/yyyy");