简体   繁体   中英

How to convert DateTime Column to String during Runtime in DataTable

Could you please suggest how to convert Date Column to ShortDateString During Runtime in this below DataTable

    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    static DataTable GetTable1()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

    }

Simply change the Date column to

  table.Columns.Add("Date", typeof(string));

and in the Add call change the DateTime.Now to

  table.Rows.Add(22, "Indocin", "David", DateTime.Now.ToShortDateString());

But this seems to be logically incorrect. Why do you want to have a date representend internally as a string. Leave it as a Date and, when you need to display it apply the formatting rules provided by the user interface object that you want to use to display your datatable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM