Databind将Dictionary列入GridView

时间:2011-04-19 08:17:12

标签: asp.net

我想将List of Dictionary绑定到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绑定到该Object(或System.Collections.HashTable)。

修改

极客的字典工作解决方案但不适用于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();

它只显示Test属性。

由于

1 个答案:

答案 0 :(得分:1)

查看以下代码。

&lt;%@ Page Language =“C#”AutoEventWireup =“true”CodeFile =“Default.aspx.cs”Inherits =“_ Default”%&gt;

    <!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

如果您想使用Hashtable,请执行以下操作

创建一个名为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();