Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将词典列表数据绑定到GridView中_Asp.net - Fatal编程技术网

Asp.net 将词典列表数据绑定到GridView中

Asp.net 将词典列表数据绑定到GridView中,asp.net,Asp.net,我想将字典列表绑定到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"; l

我想将字典列表绑定到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();
当我这样修改映射时:

var result = liste.Select(map => new
{
    IdClient = map["Denomination"],
    Denomination = map["Denomination2"],
    Test = "test"
}).ToList();
它将仅显示测试属性


谢谢

查看以下代码

然后按如下方式查询列表

 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();
var liste=new List();
var table=新的Hashtable();
表[“面额”]=“a”;
表[“面额2”]=“安”;
添加(表);
var result=liste.Select(map=>newclient()
{
IdClient=map[“面额”].ToString(),
面额=地图[“面额2”]。ToString()
});
gvClients.DataSource=结果;
gvClients.DataBind();

查看以下代码

然后按如下方式查询列表

 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();
var liste=new List();
var table=新的Hashtable();
表[“面额”]=“a”;
表[“面额2”]=“安”;
添加(表);
var result=liste.Select(map=>newclient()
{
IdClient=map[“面额”].ToString(),
面额=地图[“面额2”]。ToString()
});
gvClients.DataSource=结果;
gvClients.DataBind();

自动数据绑定无法执行此操作。您必须使用PropertyDescriptor将项转换为表。根据我的需要,这看起来有点复杂。你有一个简短的例子吗?你使用的是哪个版本的.net?自动数据绑定不能做到这一点。您必须使用PropertyDescriptor将项转换为表。根据我的需要,这看起来有点复杂。你有一个简短的例子吗?你使用的是哪个版本的.net?我可能不太准确。我有多个属性要显示,例如:Property1,Property2,Property3,我的列表更像是一个数据表。。。(它是由我的ORM生成的)我不明白你的意思…你能分享数据的确切结构吗?我再次刷新了这个问题,它可以工作,但我正在哈希表上寻找解决方案。再说一次,谢谢。我可能不太准确。我有多个属性要显示,例如:Property1,Property2,Property3,我的列表更像是一个数据表。。。(它是由我的ORM生成的)我不明白你的意思…你能分享数据的确切结构吗?我再次刷新了这个问题,它可以工作,但我正在哈希表上寻找解决方案。再次感谢你。
var result = liste.Select(map => new
{
    IdClient = map["Denomination"],
    Denomination = map["Denomination2"],
    Test = "test"
}).ToList();
    <!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();

        }

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