字典<OBJ,列表<OBJ>&GT;进入GridView数据源?</obj,列出<obj>

时间:2013-12-08 20:00:18

标签: c# asp.net gridview dictionary

是否有一种干净,简单的方法可以将Dictionary<obj,List<obj2>>放入ASP.NET中的GridView数据源中?如果我只是将字典按原样放入GridView并且正在执行Eval("Key")Eval("Value"),我将在Eval("Value")列中找到对象名称。我正在寻找一个在Eval("Value")字段中提供逗号分隔值的解决方案。

GridView的:

<SharePoint:SPGridView
    id="GvItems"
    runat="server"
    AutoGenerateColumns="false"
    width="100%"
    AllowSorting="True" OnRowDataBound="GvItems_OnRowDataBound" >
<AlternatingRowStyle CssClass="ms-alternatingstrong" />
<Columns>
    <asp:TemplateField ItemStyle-CssClass="ms-cbp" HeaderStyle-CssClass="ms-cbp" ItemStyle-VerticalAlign="Top">
        <HeaderTemplate>
            <asp:CheckBox ID="chkBoxSL" 
                runat="server"
                AutoPostBack="true"
                OnCheckedChanged="chkBoxSL_CheckedChanged"
                style="margin-top:-1px; margin-bottom:-1px;" />
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="chkId" 
                runat="server" 
                class="padding-right: 15px;padding-top: 0px;padding-bottom: 0px" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Web Application Zone" HeaderStyle-Width="50%" HeaderStyle-CssClass="ms-vh2-nofilter-perm" SortExpression="Key">
        <ItemTemplate>
            <asp:Label ID="lblZone" runat="server" Text='<%# Eval("Key") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Crawl Target" HeaderStyle-Width="50%" HeaderStyle-CssClass="ms-vh2-nofilter-perm" SortExpression="Value">
        <ItemTemplate>
            <asp:Label ID="lblServer" runat="server" Text='<%#Eval("Value") %>' />          
        </ItemTemplate>
    </asp:TemplateField>

</Columns>

代码隐藏:

{
//...
var sds = new Dictionary<SPUrlZone, List<Uri>>();

foreach (var t in webApp.SiteDataServers)
{
    var uriList = new List<Uri>();
    foreach (var v in t.Value)
       {
           uriList.Add(v);
       }

   sds.Add(t.Key, uriList);
}
//...
GvItems.DataSource = sds;
GvItems.DataBind();
}
    protected void GvItems_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            Label label = (Label)e.Row.FindControl("lblServer");

            // extract list items

            string[] uris = ((KeyValuePair<SPUrlZone, List<Uri>>)e.Row.DataItem).Value.Select(x => x.ToString()).ToArray();
            label.Text = string.Join(",", uris);
        }
    }

1 个答案:

答案 0 :(得分:3)

检查MSDN http://msdn.microsoft.com/en-us/library/4hx47hfe%28v=vs.110%29.aspx。目前您只访问List,这就是为什么不按您的意愿进行评估的原因。您可以选择在codebehind中编写一个方法并手动完成 - 我个人更喜欢这种方式。

我为你做了一个小例子:

<强>标记

 <asp:GridView runat="server" ID="grid1">
            <Columns>
                <asp:BoundField HeaderText="Key" DataField="Key"></asp:BoundField>
                <asp:TemplateField HeaderText="Value">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

代码

protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, List<Person>> data = new Dictionary<string, List<Person>>();
        data.Add("Managers", new List<Person>() { new Person() { Age = 38, Name = "Bob" }, new Person() { Age = 45, Name = "Stephen" } });
        data.Add("Developers", new List<Person>() { new Person() { Age = 25, Name = "Jake" }, new Person() { Age = 31, Name = "John" }, new Person() { Age = 27, Name = "Matthew" } });

        grid1.RowDataBound += grid1_RowDataBound;
        grid1.DataSource = data;
        grid1.DataBind();
    }

    void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            Label label = (Label)e.Row.FindControl("Label1");

            // extract list items
            string[] names = ((KeyValuePair<string, List<Person>>) e.Row.DataItem).Value.Select(x => x.Name).ToArray();
            label.Text = string.Join(",", names);
        }
    }

    protected class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

或者您可以绑定从开头直接分隔的逗号值...取决于您的方案。