none
DataTable convert some row to TimeSpan RRS feed

  • Question

  • Hallo

    Hope all are well.

    We get data from sql into a Datatable, but the datatypes in some columns is int and would like to convert some of the rows in the datatable to the below. 

    Below snippet of the records in SQL which is int.. 

    And the idea we are trying to make work. (Could probably copy each row into another Datatable, but if possible just want to use the one datatable.

     
           public DataTable GetMonthWorkingHoursPerID (TimeSheetBO TimeVar)
            {
                DataTable dt = new DataTable();
                DataTable dmun = new DataTable();
    
                dt = TimeRecords.GetWorkingHoursPerSpecMonthAndEmpID(TimeVar);
    
                foreach (DataRow dr in dt.Rows)
                {
                    dt.Columns["Normal HR"] = TimeSpan.FromMinutes(dt.Columns["Normal HR"]);
                    dt.Columns["Not Approved"] = TimeSpan.FromMinutes(dt.Columns["NotApproved"]);
                }
    
    
                return dt;
            }



    Primkey	EmpID	Site	Clock Type	Clock In	Clock Out	Total HR	Normal HR	Not Approved	Approved	Special	Holiday
    1058	1	Amrod Midrand	Public Holiday	2018-10-01 08:00:00.000	2018-10-01 16:30:00.000	08:30:00.0000000	0	0	0	450	0
    1059	1	Amrod Midrand	Public Holiday	2018-10-27 08:00:00.000	2018-10-27 16:30:00.000	08:30:00.0000000	0	0	0	450	0
    1060	1	Amrod Midrand	Public Holiday	2018-10-27 08:00:00.000	2018-10-27 16:30:00.000	08:30:00.0000000	0	0	0	225	0
    1061	1	Amrod Midrand	Public Holiday	2018-10-27 08:00:00.000	2018-10-27 16:30:00.000	08:30:00.0000000	0	0	0	675	0
    1062	1	Amrod Midrand	Public Holiday	2018-10-27 08:00:00.000	2018-10-27 16:30:00.000	08:30:00.0000000	0	0	0	765	0
    1063	1	Amrod Midrand	Public Holiday	2018-10-27 08:00:00.000	2018-10-27 16:00:00.000	08:00:00.0000000	0	0	0	720	0
    1064	1	Amrod Midrand	Public Holiday	2018-10-28 08:00:00.000	2018-10-28 16:00:00.000	08:00:00.0000000	0	0	0	960	0
    1065	1	Amrod Midrand	Special Permission	2018-10-28 08:00:00.000	2018-10-28 16:00:00.000	08:00:00.0000000	0	0	0	0	840
    1066	1	Amrod Midrand	Special Permission	2018-10-27 08:00:00.000	2018-10-27 16:00:00.000	08:00:00.0000000	0	0	0	0	840
    1067	1	Amrod Midrand	NotApproved	2018-10-24 11:19:54.000	2018-10-24 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1068	1	Amrod Midrand	NotApproved	2018-10-16 11:19:54.000	2018-10-16 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1069	1	Amrod Midrand	NotApproved	2018-10-16 11:19:54.000	2018-10-16 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1070	1	Amrod Midrand	NotApproved	2018-10-29 11:19:54.000	2018-10-29 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1071	1	Amrod Midrand	NotApproved	2018-10-29 11:19:54.000	2018-10-29 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1072	1	Amrod Midrand	NotApproved	2018-10-29 11:19:54.000	2018-10-29 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1073	1	Amrod Midrand	NotApproved	2018-10-08 11:19:54.000	2018-10-08 23:19:54.000	12:00:00.0000000	480	180	0	0	0
    1074	1	Amrod Midrand	NotApproved	2018-10-09 08:00:00.000	2018-10-09 16:30:00.000	08:30:00.0000000	450	0	0	0	0
    1075	1	Amrod Midrand	NotApproved	2018-10-09 08:00:00.000	2018-10-09 16:30:00.000	08:30:00.0000000	450	0	0	0	0
    1076	1	Amrod Midrand	Approved	2018-10-27 00:23:44.330	2018-10-27 00:23:44.333	00:00:00.0046245	0	0	0	0	0


    labjac

    Saturday, October 27, 2018 4:14 AM

All replies

  • Hi labjac,

    Based on your description and related code, I would suggest that you could add two column, which store timespan in it, like this:

    public DataTable GetMonthWorkingHoursPerID(TimeSheetBO TimeVar)
    {
                DataTable dt = new DataTable();
                DataTable dmun = new DataTable();
    
                dt = TimeRecords.GetWorkingHoursPerSpecMonthAndEmpID(TimeVar);
    
                dt.Columns.Add("TimeSpanNormalHR", typeof(TimeSpan));
                dt.Columns.Add("TimeSpanNotApproved", typeof(TimeSpan));
    
                foreach (DataRow dr in dt.Rows)
                {
                    dr["TimeSpanNormalHR"] = TimeSpan.FromMinutes(Convert.ToDouble(dr["Normal HR"]));
                    dr["TimeSpanNotApproved"] = TimeSpan.FromMinutes(Convert.ToDouble(dr["NotApproved"]));
                }
                return dt;
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 29, 2018 5:08 AM
    Moderator