Is there a clean, easy way to get a Dictionary<obj,List<obj2>> into a GridView Datasource in ASP.NET? If I just throw the Dictionary as-is into the GridView and am doing an Eval("Key") and Eval("Value"), I'll end up with the object name in the Eval("Value") column. I'm looking for a solution that provides a comma separated value in the Eval("Value") field.
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>
Codebehind:
{
//...
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);
}
}