【问题标题】:GridView Formatting in aspxaspx 中的 GridView 格式设置
【发布时间】:2015-03-23 07:27:52
【问题描述】:

我在数据库中有一个表,我想以不同的方式在前端显示它。但我没有得到任何帮助来管理这个并展示不同的方式。

数据库表是:

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();

        }
    }

【问题讨论】:

    标签: c# asp.net sql-server gridview


    【解决方案1】:

    您想要的是透视您的数据或透视表。我不知道哪个是英语中的技术正确术语。但至于您的问题,这与格式化您的 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

    /编辑: 总而言之,您的查询应如下所示:

    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();
    }
    

    【讨论】:

    • 感谢您的帮助,但商品名称不固定,商品名称也由用户插入。
    • 为什么在我回答之前你的这部分问题没有出现?
    • 对不起,我已经和你共享了一个数据库表,表内记录是动态的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多