2009-12-14 90 views
0

I組我的數據如下ASP.NET/C# LINQ的通過長度綁定到的GridView

int[] a = { 1, 2, 3,45,45,100,566}; 

var result = 
a.GroupBy(x => x.ToString().Length). 
Select(d => new { Key = d.Key, Grp = d }); 

我的BulletedList嵌套在GridView中(如放置模板字段)顯示的項目,這是結合的方式GridView顯示「Key」時BulletedList。

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

回答

2

設置DataKeyNames到您的鍵名

例如:

<asp:gridview id="CustomersGridView" 
     datasourceid="CustomersSource" 
     autogeneratecolumns="true" 
     emptydatatext="No data available." 
     autogenerateselectbutton="true"  
     datakeynames="CustomerID" 
+0

+1爲正確的答案,但作爲有關的問題:添加GridView1.DataKeyNames =「Key」;就在DataBind()之前 –

0

一個GridView控件裏綁定到一個項目符號列表(工作原理類似於任何控制,當然)

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     RadioButtonList list = (RadioButtonList)e.Row.FindControl("rbList"); 
     if(list != null) 
     { 
     list.DataSource = mysource; 
     list.DataBind(); 
     } 
    } 
    } 

確保將事件添加到GridView。