将字典词典数据绑定到GridView中 [英] Databind List of Dictionnary into a GridView

查看:77
本文介绍了将字典词典数据绑定到GridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字典列表绑定到GridView.

I'd like to bind a List of Dictionary to a 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).

How can I bind the GridView to that Object (or a System.Collections.HashTable).

修改

极客针对Dictionary的解决方案,但不适用于HashTable,有解决方案吗?

The geek's solution for Dictionary work but not for HashTable, is there a solution ?

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.

当我像这样修改映射时:

When I modify the mapping like this :

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

它将仅显示Test属性.

It will show only the Test property.

谢谢

推荐答案

查看以下代码.

<%@页面语言="C#" AutoEventWireup ="true" CodeFile ="Default.aspx.cs" Inherits ="_ Default"%>

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

    <!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();
                }

            }
        } 

修改

我已根据您的要求编辑了代码.请尝试使用此代码,如果遇到任何问题,请告诉我.

I have edited the code as per your requirements.Please try this code and let me know if you experience any problems .

<%@ 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

如果要使用哈希表,请执行以下操作

If you want to use Hashtable then do the following

创建一个名为Client的新类(随心所欲)

Create a new class named Client (what ever you want)

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

然后按如下所示查询列表

and then query the list as follows

 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();

这篇关于将字典词典数据绑定到GridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆