简体   繁体   中英

How to Pivote grid view in C#

Hi I have a list and it is set as a data source for a grid view. My grid view looks like this.

      Green| Gold | Grade  
       ----|----  | -------  
       2   | 1    | 100  
       3   | 1    | 101  
       -   | 3    | 102  
       3   | -    | 104  
       -   | -    | 105  
       8   | 5    | Total  

But I want like this

  Grade   |100  |101 |102 |103 |104 |105 |Total  
--------- |---- |----|----|----|----|----|-----  
  Green   |2    |3   |-   |3   |8   |-   |8  
  Gold    |1    |1   |3   |-   |3   |-   |5  

This is My List class

public class Value
{
    public string Gold { get; set; }
    public string Grade { get; set; }
    public string Green { get; set; }
}

public class GetCACPopGuideResult
{
    public object Type { get; set; }
    public List<Value> Value { get; set; }
}

This is what I have done in the code

GetCACPopGuideResult result = getvalue();// Get the values to display
GridView1.DataSource= result.Value;

Public GetCACPopGuideResult getvalue()
{
        string URL= myURL;
        var cli = new RestClient(URL);
        IRestRequest auctionRequest = new RestRequest(Method.GET);
        IRestResponse responseCustomer = cli.Execute(auctionRequest);
        return responseCustomer.Content;
}

Is there a way I can do this in C#? Please help me.

Thanks in advance

You can't create dynamic objects in LINQ.

What you should do is convert the data you receive in a Dictionary like this example:

var dictionary = value
    .GroupBy(v => v.Grade)
    .ToDictionary(g => g.Key, 
        g => new 
            {
                g.Select(x => x.Gold).FirstOrDefault(),
                g.Select(x => x.Green).FirstOrDefault()
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM