Как отобразить день в строке, используя GridView в ASP.NET ?

#c# #asp.net

#c# #asp.net

Вопрос:

Я занимаюсь некоторой разработкой таблицы, в которой я могу отображать данные в GridView. Это была таблица размером 12 на 31 день, в которой каждая цена должна отображать данные в соответствующей строке и столбце дня. Например, для января необходимо отобразить строку для января и так далее. Приведенные ниже примеры данных — это точные данные, мне нужны только цены, которые нужно отобразить в строке. Примерный вид.

       1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | and so on to 31
January  100|100|100|100|100|100|100|100|100|100 | and so on to 31

 protected void Page_Load(object sender, EventArgs e)
        {
            string[] months = new string[]
            {
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
            };

            using (var reader = new StreamReader(@"C:ProjectsTestSampleTestSampleRenderJsonToViewprices.json"))
            {
                string jsonObject = reader.ReadToEnd();

                var prices = JsonConvert.DeserializeObject<List<ComparePrice>>(jsonObject);

                if (!IsPostBack)
                {
                    DataTable dataTable = new DataTable();

                    dataTable.Columns.AddRange(new DataColumn[32]
                    {
                        new DataColumn("Months", typeof(string)),
                        new DataColumn("Day1", typeof(string)),
                        new DataColumn("Day2", typeof(string)),
                        new DataColumn("Day3", typeof(string)),
                        new DataColumn("Day4", typeof(string)),
                        new DataColumn("Day5", typeof(string)),
                        new DataColumn("Day6", typeof(string)),
                        new DataColumn("Day7", typeof(string)),
                        new DataColumn("Day8", typeof(string)),
                        new DataColumn("Day9", typeof(string)),
                        new DataColumn("Day10",typeof(string)),
                        new DataColumn("Day11",typeof(string)),
                        new DataColumn("Day12",typeof(string)),
                        new DataColumn("Day13",typeof(string)),
                        new DataColumn("Day14",typeof(string)),
                        new DataColumn("Day15",typeof(string)),
                        new DataColumn("Day16",typeof(string)),
                        new DataColumn("Day17",typeof(string)),
                        new DataColumn("Day18",typeof(string)),
                        new DataColumn("Day19",typeof(string)),
                        new DataColumn("Day20",typeof(string)),
                        new DataColumn("Day21",typeof(string)),
                        new DataColumn("Day22",typeof(string)),
                        new DataColumn("Day23",typeof(string)),
                        new DataColumn("Day24",typeof(string)),
                        new DataColumn("Day25",typeof(string)),
                        new DataColumn("Day26",typeof(string)),
                        new DataColumn("Day27",typeof(string)),
                        new DataColumn("Day28",typeof(string)),
                        new DataColumn("Day29",typeof(string)),
                        new DataColumn("Day30",typeof(string)),
                        new DataColumn("Day31",typeof(string))
                    });

                    for (int i = 0; i < prices.Count; i  )
                    {
                        foreach (DataRow dataRow in dataTable.Rows)
                        {
                            dataRow["Day1"] = prices[i].Prices[0].ToString();
                            dataRow["Day2"] = prices[i].Prices[1].ToString();
                            dataRow["Day3"] = prices[i].Prices[2].ToString();
                            dataRow["Day4"] = prices[i].Prices[3].ToString();
                            dataRow["Day5"] = prices[i].Prices[4].ToString();
                            dataRow["Day6"] = prices[i].Prices[5].ToString();
                            dataRow["Day7"] = prices[i].Prices[6].ToString();
                            dataRow["Day8"] = prices[i].Prices[7].ToString();
                            dataRow["Day9"] = prices[i].Prices[8].ToString();
                            dataRow["Day10"] = prices[i].Prices[9].ToString();
                            dataRow["Day11"] = prices[i].Prices[10].ToString();
                            dataRow["Day12"] = prices[i].Prices[11].ToString();
                            dataRow["Day13"] = prices[i].Prices[12].ToString();
                            dataRow["Day14"] = prices[i].Prices[13].ToString();
                            dataRow["Day15"] = prices[i].Prices[14].ToString();
                            dataRow["Day16"] = prices[i].Prices[15].ToString();
                            dataRow["Day17"] = prices[i].Prices[16].ToString();
                            dataRow["Day18"] = prices[i].Prices[17].ToString();
                            dataRow["Day19"] = prices[i].Prices[18].ToString();
                            dataRow["Day20"] = prices[i].Prices[19].ToString();
                            dataRow["Day21"] = prices[i].Prices[21].ToString();
                            dataRow["Day22"] = prices[i].Prices[22].ToString();
                            dataRow["Day23"] = prices[i].Prices[23].ToString();
                            dataRow["Day24"] = prices[i].Prices[24].ToString();
                            dataRow["Day25"] = prices[i].Prices[25].ToString();
                            dataRow["Day26"] = prices[i].Prices[26].ToString();
                            dataRow["Day27"] = prices[i].Prices[27].ToString();
                            dataRow["Day28"] = prices[i].Prices[28].ToString();
                            dataRow["Day29"] = prices[i].Prices[29].ToString();
                            dataRow["Day30"] = prices[i].Prices[30].ToString();
                            dataRow["Day31"] = prices[i].Prices[31].ToString();
                        }

                        foreach (var price in prices[i].Prices)
                        {
                            dataTable.Rows.Add(months[i], price);
                        }
                    }

                    GridViewTest.DataSource = dataTable;
                    GridViewTest.DataBind();
                }

            }
        }
  

Data JSON

 [
  {
    "Month": 1,
    "Prices": [
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-01T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-02T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-03T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-04T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-05T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-06T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-07T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-08T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-09T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-10T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-11T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-12T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-13T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-14T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-15T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-16T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-17T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-18T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-19T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-20T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-21T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-22T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-23T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-24T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-25T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-26T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-27T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 2,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-28T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 1,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-29T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 1,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-30T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      },
      {
        "RoomsAllocated": 2,
        "Price": 100.0,
        "MinimumStay": 1,
        "Inclusion": "breakfast and lunch",
        "Date": "2018-01-31T00:00:00",
        "RoomsAllocatedDirty": true,
        "PriceDirty": true,
        "StopSellDirty": true,
        "MinimumStayDirty": true,
        "InclusionDirty": true
      }
    ]
  }
  

Ответ №1:

В течение многих лет я создавал систему для отеля с веб-сайтом, и у меня была похожая ситуация, может быть, у вас что-то получится, разница в том, что так же, как у вас есть цены на номера в файле JSON, я поместил это в таблицу, в итоге было больше таблиц, и цена за номер была рассчитана с учетом факторов сезона, дней вперед, типа номера, и если между ними было несколько отпусков. там, где у меня есть var Q, вы можете поместить свой запрос JSON, это код:

  public DataTable DoGrid()
    {
        DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
        DataTable dt = new DataTable();
        DataRow row;
        dt.Columns.Add("No", typeof(int));
        dt.Columns.Add("Year", typeof(string));
        dt.Columns.Add("Month", typeof(string));
        dt.Columns.Add("Date", typeof(string));
        dt.Columns.Add("Occupation", typeof(string));
        for(day = CalFrom.Date; day <= CalTo; day = day.AddDays(1))
        {
            var Q = db.Reservas.Where(c => c.CheckIn <= day amp;amp; c.CheckOut > day amp;amp; c.Cancelado == false);
            string MyMonth = dtfi.GetMonthName(day.Month).ToString();
            row = dt.NewRow();
            row["No"] = dt.Rows.Count   1;
            row["Year"] = day.Year.ToString();
            row["Month"] = MyMonth;
            row["Date"] = day.ToString("dd/MM/yyyy");
            row["Occupation"] = Q.Count();
            dt.Rows.Add(row);
        }
        return dt;
    }

    public DateTime day { get; set; }

    public DateTime CalFrom
    {
        get
        {
            return DpFrom.Value.Value;
        }
    }

    public DateTime CalTo
    {
        get
        {
            return DpTo.Value.Value;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = DoGrid();
        GridView1.DataBind();
    }
}
  

И это результат кода
Пример таблицы данных