1

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

The code in the 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>

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

edit

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

I've this error :

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

It will show only the Test property.

Thanks

P. Sohm
  • 2,842
  • 2
  • 44
  • 77

1 Answers1

1

Check out the following code.

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

            }
        } 

Edit

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

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();
santosh singh
  • 27,666
  • 26
  • 83
  • 129
  • well I wasn't probably enouth accurate. I've multiple property to show, ex : Property1, Property2, Property3, my List> is more like a DataTable ... (it was generated by my ORM) – P. Sohm Apr 19 '11 at 11:52
  • I am not getting you ...could you please share the exact structure of the data – santosh singh Apr 19 '11 at 12:02
  • I refreshed again the question, it works but I'm looking for a solution on the hashtable. and again, thank you. – P. Sohm Apr 20 '11 at 08:21