2011-04-19 84 views
1

我想將字典列表綁定到GridView。Databind將字典列表綁定到GridView中

var liste = new List<Dictionary<string, string>>(); 
var dictionary = new Dictionary<string,string>(); 
dictionary["Id"] = "111"; 
dictionary["Description"] = "text to show"; 
dictionary["OtherInfo"] = "other text"; 
liste.Add(dictionary); 
gvClients.DataSource = liste; 
gvClients.DataBind(); 

在ASPX的代碼:

<asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="true" GridLines="None" 
    AllowPaging="True" CssClass="gridview" AlternatingRowStyle-CssClass="alt" Width="80%"> 
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
    <Columns> 
    <asp:TemplateField HeaderText="PropertyName"> 
     <ItemTemplate> 
     <asp:Label ID="Label1" runat="server" Text='<%# Eval("Description") %>'></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="PropertyName"> 
     <ItemTemplate> 
     <asp:Label ID="Label2" runat="server" Text='<%# Eval("OtherInfo") %>'></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

如何可以結合在GridView到該對象(或一System.Collections.HashTable)。

編輯

的怪胎對字典的工作,但不是哈希表的解決方案,有沒有解決方案?

var liste = new List<System.Collections.Hashtable>(); 
var table = new System.Collections.Hashtable(); 
table["Denomination"] = "a"; 
table["Denomination2"] = "an"; 
liste.Add(table); 

var result = liste.Select(map => new 
{ 
    IdClient = map["Denomination"], 
    Denomination = map["Denomination2"] 
}).ToList(); 

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

我有此錯誤:

The data source for GridView with id 'gvClients' did not have any properties or attributes from which to generate columns. 

當我修改這樣的映射:

var result = liste.Select(map => new 
{ 
    IdClient = map["Denomination"], 
    Denomination = map["Denomination2"], 
    Test = "test" 
}).ToList(); 

這將只顯示測試性能。

謝謝

+1

自動數據綁定無法做到這一點。您必須使用PropertyDescriptor將項目轉換爲表格。 – 2011-04-19 08:44:34

+0

它看起來有點複雜,我的需要。你有一個簡短的例子嗎? – 2011-04-19 09:21:27

+0

你使用哪個版本的.net? – 2011-04-19 09:51:50

回答

1

看看下面的代碼。

<%@頁面語言= 「C#」 AutoEventWireup = 「真」 的CodeFile = 「Default.aspx.cs」 繼承= 「_默認」 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title></title> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div> 
      <asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="false" GridLines="None" 
       CssClass="gridview" AlternatingRowStyle-CssClass="alt" Width="80%"> 
       <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
       <Columns> 
        <asp:BoundField DataField="Key" /> 
        <asp:BoundField DataField="Value" /> 
       </Columns> 
      </asp:GridView> 
     </div> 
     </form> 
    </body> 
    </html> 




using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Web; 
     using System.Web.UI; 
     using System.Web.UI.WebControls; 
     using System.Collections.Specialized; 

     public partial class _Default : System.Web.UI.Page 
     { 


      protected void Page_Load(object sender, EventArgs e) 
      { 
       if (!IsPostBack) 
       { 
        var liste = new List<Dictionary<string, string>>(); 
        var dictionary = new Dictionary<string, string>(); 
        dictionary["PropertyName"] = "text to show"; 
        var dictionary2 = new Dictionary<string, string>(); 
        dictionary2["PropertyName"] = "text to show1"; 
        liste.Add(dictionary2); 
        liste.Add(dictionary); 
        var result = liste.SelectMany(x => x); 
        gvClients.DataSource = result; 
        gvClients.DataBind(); 
       } 

      } 
     } 

編輯

我已根據您的要求編輯了代碼。請嘗試此代碼並讓我知道您是否遇到任何問題。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="true" GridLines="None" 
      CssClass="gridview" AlternatingRowStyle-CssClass="alt" Width="80%"> 
      <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
     <Columns> 
       <asp:TemplateField HeaderText="Description"> 
        <ItemTemplate> 
         <asp:Label ID="Label1" runat="server" Text='<%# Eval("Description") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="OtherInfo"> 
        <ItemTemplate> 
         <asp:Label ID="Label2" runat="server" Text='<%# Eval("OtherInfo") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 






using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
public class Client 
{ 
    public string ID { get; set; } 
    public string Description { get; set; } 
    public string OtherInfo { get; set; } 
} 
public partial class Default3 : System.Web.UI.Page 
{ 
    List<Client> list=new List<Client>(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 
      var liste = new List<Dictionary<string, string>>(); 
      var dictionary = new Dictionary<string, string>(); 
      dictionary["ID"] = "111"; 
      dictionary["Description"] = "XYZ"; 
      dictionary["OtherInfo"] = "Addd"; 
      liste.Add(dictionary); 


      var result = liste.Select(map => new Client 
      { 
       ID = map["ID"], 
       Description = map["Description"], 
       OtherInfo = map["OtherInfo"] 
      }).ToList(); 

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

     } 

    } 
} 

EDITII

如果你想使用哈希表然後執行以下操作

創建一個名爲Client的新類(你想要什麼都)

public class Client 
{ 
    public string IdClient { get; set; } 
    public string Denomination { get; set; } 
} 

,然後查詢清單如下

var liste = new List<Hashtable>(); 
      var table = new Hashtable(); 
      table["Denomination"] = "a"; 
      table["Denomination2"] = "an"; 
      liste.Add(table); 

      var result = liste.Select(map => new Client() 
      { 
       IdClient = map["Denomination"].ToString(), 
       Denomination = map["Denomination2"].ToString() 
      }); 

      gvClients.DataSource = result; 
      gvClients.DataBind(); 
+0

以及我不太準確。我有多個屬性來顯示,例如:Property1,Property2,Property3,我的List >更像是一個DataTable ...(它是由我的ORM生成的) – 2011-04-19 11:52:46

+0

我沒有收到你.. 。請你分享數據的確切結構 – 2011-04-19 12:02:42

+0

我刷新了這個問題,謝謝你的回答 – 2011-04-19 14:10:22