如何在ASP.NET中使用GridView将日期绘制成一行?

时间:2019-03-19 08:57:14

标签: c# asp.net

我正在做一些表格开发工作,可以在其中将数据绘制到GridView中。这是一个12 x 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:\Projects\TestSample\TestSample\RenderJsonToView\prices.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();
                }

            }
        }

数据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 个答案:

答案 0 :(得分:0)

多年来,我已经使用该网站为一家酒店创建了一个系统,并且遇到了类似的情况,也许您有所收获,不同之处在于,就像您将房价包含在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 && c.CheckOut > day && 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();
    }
}

这是代码的结果 Data Table Example