Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="table"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="dateType">custom、day、month、hour</param>
        /// <returns></returns>
        public static DataTable TableAddBlankRow(DataTable table, DateTime startTime, DateTime endTime, string dateType)
        {
            table.DefaultView.Sort = "DATA_TIME_ORDER ASC";
            DateTime currentTime = startTime;

            while (currentTime <= endTime)
            {
                bool exit = false;
                foreach (DataRow row in table.Rows)
                {
                    DateTime rowDataTime;
                    if (DateTime.TryParse(StringHelper.DynamicToString(row["DATA_TIME"]), out rowDataTime))
                    {
                        if ("custom" == dateType || "hour" == dateType)
                        {
                            if (rowDataTime.ToString("yyyy-MM-dd HH") == currentTime.ToString("yyyy-MM-dd HH"))
                            {
                                exit = true;
                                break;
                            }
                        }
                        if ("day" == dateType)
                        {
                            if (rowDataTime.ToString("yyyy-MM-dd") == currentTime.ToString("yyyy-MM-dd"))
                            {
                                exit = true;
                                break;
                            }
                        }
                        if ("month" == dateType)
                        {
                            if (rowDataTime.ToString("yyyy-MM") == currentTime.ToString("yyyy-MM"))
                            {
                                exit = true;
                                break;
                            }
                        }
                        if ("year" == dateType)
                        {
                            if (rowDataTime.ToString("yyyy") == currentTime.ToString("yyyy"))
                            {
                                exit = true;
                                break;
                            }
                        }
                    }
                }
                if (!exit)
                {
                    DataRow newRow = table.NewRow();
                    newRow["DATA_TIME"]       = currentTime;
                    newRow["DATA_TIME_ORDER"] = currentTime;
                    table.Rows.Add(newRow);
                }
                switch (dateType)
                {
                case "hour": currentTime = currentTime.AddHours(1); break;

                case "custom": currentTime = currentTime.AddHours(1); break;

                case "day": currentTime = currentTime.AddDays(1); break;

                case "month": currentTime = currentTime.AddMonths(1); break;

                case "year": currentTime = currentTime.AddYears(1); break;

                default: currentTime = currentTime.AddYears(100); break;
                }
            }
            DataTable resultTable = table.DefaultView.ToTable();

            return(resultTable);
        }