2014-01-20 68 views
1

我從我的數據庫中檢索DataSet,但我想以不同的方式呈現它,然後返回它。我目前有行看起來像這個第一個表,我在gridview中顯示這些。將數據集綁定到gridview之前如何重構數據集?

我現在想要重構數據,以便輸出將像第二個表一樣。我並不總是得到相同的「周」,所以我必須檢查我有哪些星期,並遍歷它們來重建數據。我通過「var結果」來做到這一點,但我不知道如何正確使用返回周的結果來構建我的gridview。

|------------------------------------------| 
| Ansvarig | Name   | Week | Tid | 
|------------------------------------------| 
| John Doe | John Andersson | 4  | 29,0 | 
| John Doe | John Andersson | 5  | 0,0 | 
| John Doe | John Andersson | 5  | 0,0 | 
| John Doe | John Andersson | 13 | 8,0 | 
| John Doe | Anders Cameron | 4  | 8,0 | 
| John Doe | Anders Cameron | 12 | 11,0 | 
| John Doe | Steve Smith | 4  | 8,0 | 
| John Doe | Steve Smith | 6  | 0,0 | 
| John Doe | Steve Smith | 6  | 0,0 | 
| John Doe | Steve Smith | 7  | 0,0 | 


|-----------------------------------------------------------------------------------------| 
| Ansvarig | Name   | Week 4 | Week 5 | Week 6 | Week 7 | Week 12 | Week 13 | 
|-----------------------------------------------------------------------------------------| 
| John Doe | John Andersson | 29,0 | 0,0  | 0,0  | 0,0  |   | 8,0  | 
| John Doe | John Andersson | 29,0 | 0,0  | 0,0  | 0,0  |   | 8,0  | 
| John Doe | Anders Cameron | 8,0 |   |   |   | 11,0  |   | 
| John Doe | Steve Smith | 8,0 |   | 0,0  | 0,0  |   |   | 
| John Doe | Steve Smith | 8,0 |   | 0,0  | 0,0  |   |   | 


protected void update_Click1(object sender, EventArgs e) 
{ 
    TimeView view = new TimeView(); 
    DataSet ds = view.TimeOverview(fromDatum, tomDatum, ansvarig, anvandare); 



    GridView1.DataSource = ds; 
    GridView1.DataBind(); 


    DataView dv = new DataView(ds.Tables[0]); 
    dv.Sort = "Vecka"; 

    var result = from row in ds.Tables[0].AsEnumerable() 
       where 1 == 1 
       group row by new { weeknumber = row["Week"] } into g 
       select new 
       { 
        weekMax = g.Max(x => x["Week"]), 
       }; 


} 

回答

2

您需要進行分組和旋轉才能得到結果。

  1. 你需要一個新的對象列表或DataTable存儲的最終結果
  2. 您需要確定獨特的命名對(Ansvarig /名稱)
  3. 您需要動態地添加一週列
  4. 你需要返回結果DataTable或對象列表

一種解決方案看起來像下面這樣:

using System; 
using System.Data; 
using System.Xml; 
using System.Data.Linq; 
using System.Data.DataSetExtensions; 
using System.Linq; 

public class Program 
{ 
    public void Main() 
    { 
     var results = GetResults(GetTestData()); 

     foreach(DataColumn dc in results.Columns) 
     { 
      Console.Write("{0},", dc.ColumnName); 
     } 

     Console.WriteLine(); 

     foreach(DataRow dr in results.Rows) 
     { 
      foreach(DataColumn dc in results.Columns) 
      { 
       Console.Write("{0},", dr[dc.ColumnName]); 
      } 
      Console.WriteLine(); 
     } 
    } 

    private DataTable GetResults(DataSet ds) 
    { 
     var result = (from row in ds.Tables[0].AsEnumerable() 
       let ansvarig = row.Field<string>("Ansvarig") 
       let name = row.Field<string>("Name") 
       let week = row.Field<int>("Week") 
       let tid = row.Field<double>("Tid") 
       group row by new { ansvarig, name, week } into grp 
       select new 
       { 
        Ansvarig = grp.Key.ansvarig, 
        Name = grp.Key.name, 
        Week = grp.Key.week, 
        Total = grp.Sum(r => r.Field<double>("Tid")) 
       }).ToList(); 

     var uniqueWeeks = result 
          .Select(item => new { Week = item.Week }) 
          .OrderBy(x => x.Week) 
          .Distinct() 
          .ToList(); 

     var dt = new DataTable(); 
     dt.Columns.Add(new DataColumn("Ansvarig", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("Name", typeof(System.String))); 

     // add week columns 
     foreach(var item in uniqueWeeks) 
     { 
      Console.WriteLine("Week: {0}", item.Week); 
      dt.Columns.Add(new DataColumn(string.Format("Week {0}", item.Week), typeof(System.String))); 
     } 

     // add rows 
     foreach (var item in result) 
     { 
      var foundRow = dt.AsEnumerable().FirstOrDefault(r => r.Field<string>("Ansvarig") == item.Ansvarig && r.Field<string>("Name") == item.Name); 
      if (foundRow == null) 
      { 
       var dr = dt.NewRow(); 
       dr["Ansvarig"] = item.Ansvarig; 
       dr["Name"] = item.Name; 
       dr[string.Format("Week {0}", item.Week)] = item.Total; 
       dt.Rows.Add(dr); 
      } 
      else 
      { 
       foundRow[string.Format("Week {0}", item.Week)] = item.Total; 
      } 
     } 

     return dt; 
    } 

    private DataSet GetTestData() 
    { 
     var ds = new DataSet(); 
     var dt = new DataTable(); 
     dt.Columns.Add(new DataColumn("Ansvarig", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("Name", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("Week", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("Tid", typeof(System.Double))); 

     var dr = dt.NewRow(); 

     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "John Andersson"; 
     dr["Week"] = 4; 
     dr["Tid"] = 29; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "John Andersson"; 
     dr["Week"] = 5; 
     dr["Tid"] = 0; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "John Andersson"; 
     dr["Week"] = 5; 
     dr["Tid"] = 0; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "John Andersson"; 
     dr["Week"] = 13; 
     dr["Tid"] = 8; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Anders Cameron"; 
     dr["Week"] = 4; 
     dr["Tid"] = 8; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Anders Cameron"; 
     dr["Week"] = 12; 
     dr["Tid"] = 11; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Steve Smith"; 
     dr["Week"] = 4; 
     dr["Tid"] = 8; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Steve Smith"; 
     dr["Week"] = 6; 
     dr["Tid"] = 0; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Steve Smith"; 
     dr["Week"] = 6; 
     dr["Tid"] = 0; 

     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr["Ansvarig"] = "John Doe"; 
     dr["Name"] = "Steve Smith"; 
     dr["Week"] = 7; 
     dr["Tid"] = 0; 

     dt.Rows.Add(dr); 

     ds.Tables.Add(dt); 

     return ds; 
    } 
} 

不完全是漂亮,但它會輸出:

Ansvarig,Name,Week 4,Week 5,Week 6,Week 7,Week 12,Week 13, 
John Doe,John Andersson,29,0,,,,8, 
John Doe,Anders Cameron,8,,,,11,, 
John Doe,Steve Smith,8,,0,0,,, 

工作小提琴:http://dotnetfiddle.net/UgpXDN

+0

哇,沒想到這樣一個很好的答案,現在我只是要得到這個吸盤輸出到我的GridView控件。 :) – Kolla

+1

@Kolla我會親自嘗試並遠離DataSet並創建一個類來容納這些對象。它使整個過程更容易。然後你可以有一個通用名單。 – Junto

+0

是的,我將不得不閱讀它,因爲我不知道如何去做:) – Kolla