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"]),
};
}
哇,沒想到這樣一個很好的答案,現在我只是要得到這個吸盤輸出到我的GridView控件。 :) – Kolla
@Kolla我會親自嘗試並遠離DataSet並創建一個類來容納這些對象。它使整個過程更容易。然後你可以有一個通用名單。 –
Junto
是的,我將不得不閱讀它,因爲我不知道如何去做:) – Kolla