簡體   English   中英

ASPX中的GridView格式

[英]GridView Formatting in aspx

我在數據庫中有一個表,我想以不同的方式在前端顯示它。 但是我沒有得到任何幫助來管理它並顯示不同的方式。

數據庫表是:

Id  Month  Commodity  Amount
----------------------------
1   May     wheat      100
2   May     rice       200
3   June    wheat      400
4   July    maize      100

my result :

 Id  Month  Commodity  Amount
----------------------------
1   May     wheat      100
2   May     rice       200
3   June    wheat      400
4   July    maize      100

但是我想用以下格式的gridview顯示數據:

  Month      wheat    rice    maize
  --------------------------------
  May        100      200     
  June       400
  July                        100

我的aspx代碼:

<asp:GridView ID="grdData" runat="server">
</asp:GridView>

和aspx.cs代碼

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getdata();
        }
    }

    public void getdata()
    {
        using (GridFormatEntities context = new GridFormatEntities())
        {
            var result = (from r in context.tbl_Commodity
                          select new
                          {
                              Id=r.Id,
                              Month = r.Month,
                              Commodity = r.Commodity,
                              Amount = r.Amount
                          }).ToList();
            grdData.DataSource = result;
            grdData.DataBind();

        }
    }

您想要的是數據透視表或數據透視表。 我不知道英語是哪個技術核心術語。 但是關於您的問題,這與格式化Gridview無關,因為在將數據綁定到Gridview之前,您首先需要進行一些數據聚合。

我將您的基本查詢結果作為演示的起點,然后應用了數據透視表:

void Main()
{
    var list = new List<CustomObject>() 
    {
        new CustomObject() {Id = 1, Month ="May", Commodity ="Wheat", Amount = 100},
        new CustomObject() {Id = 2, Month ="May", Commodity ="Rice", Amount = 200},
        new CustomObject() {Id = 3, Month ="June", Commodity ="Wheat", Amount = 400},
        new CustomObject() {Id = 4, Month ="July", Commodity ="Maize", Amount = 100},
        new CustomObject() {Id = 5, Month ="August", Commodity ="Raspberry", Amount = 666},
    };

    var result = list.GroupBy (l => l.Month)
                     .Select (l => new {
                        Month = l.Key,
                        Wheat = l.Where(x => x.Commodity == "Wheat").Sum (x => x.Amount),
                        Rice = l.Where(x => x.Commodity == "Rice").Sum (x => x.Amount),
                        Maize = l.Where(x => x.Commodity == "Maize").Sum (x => x.Amount),
                        Raspberry = l.Where(x => x.Commodity == "Raspberry").Sum (x => x.Amount),
                     });

    result.Dump();
}

// Define other methods and classes here
class CustomObject
{
    public int Id { get; set; }
    public string Month { get; set; }
    public string Commodity { get; set; }
    public int Amount { get; set; }
}

輸出:

在此處輸入圖片說明

如果您使用的是LinqPad,則可以在此處下載整個查詢腳本: http ://share.linqpad.net/fqsodb.linq

/ EDIT:總而言之,您的查詢應如下所示:

using (GridFormatEntities context = new GridFormatEntities())
{
   var result = (from r in context.tbl_Commodity
                 select new
                 {
                     Id=r.Id,
                     Month = r.Month,
                     Commodity = r.Commodity,
                     Amount = r.Amount
                 })
                 .GroupBy (r => r.Month)
                 .Select (r => new {
                    Month = r.Key,
                    Wheat = r.Where(x => x.Commodity == "Rice").Sum (x => x.Amount),
                    Rice = r.Where(x => x.Commodity == "Rice").Sum (x => x.Amount),
                    Maize = r.Where(x => x.Commodity == "Maize").Sum (x => x.Amount),
                 }).ToList();
   grdData.DataSource = result;
   grdData.DataBind();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM