如何将包含列表的字典绑定到网格视图 [英] How to bind dictionary with containing list to grid view
本文介绍了如何将包含列表的字典绑定到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此字典绑定到网格视图-
How to bind this dictionary to grid view -
Dictionary<int, Actor> actors = new Dictionary<int, Actor>{
{
1,new Actor
{
ActorId = 1,
ActorName= "Rajanikant",
BirthDate =DateTime.Parse("12-12-1949"),
BirthPlace ="Bangalore",
Photo ="~/Images/1.jpg",
Movies = new List<string>{"Shivaji (2007)","Baba (2002)","ChaalBaaz (1989)"}
}
},
{
2,new Actor
{
ActorId = 2,
ActorName= "Jennifer Aniston",
BirthDate =DateTime.Parse("11-2-1969"),
BirthPlace ="Sherman Oaks",
Photo ="~/Images/2.jpg",
Movies=new List<string>{"Friends (1994)","Bruce Almighty (2003)","Just Go with It (2011)"}
}
},
};
在asp.net中使用c#
using c# in asp.net
推荐答案
您需要将GridView DataDource设置为 Dictionary.Values ,如下所示:
You need to set GridView DataDource to Dictionary.Values like below:
GridView1.DataSource = actors.Values;
GridView1.DataBind();
并且标记可能看起来像这样(感谢julealgon指向电影列表"):
EDIT : And the markup may look like this (thanks julealgon for pointing to Movies List) :
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="ActorId" HeaderText="Actor Id" />
<asp:BoundField DataField="ActorName" HeaderText="Actor Name" />
<asp:BoundField DataField="BirthDate" HeaderText="Birth Date" />
<asp:BoundField DataField="BirthPlace" HeaderText="Birth Place" />
<asp:TemplateField HeaderText="Photo" >
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%#Eval("Photo") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movies" >
<ItemTemplate>
<asp:ListBox ID="ListBox1" DataSource='<%#Eval("Movies") %>' runat="server"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
显示方式如下:
您可以下载我用来测试代码的测试项目这里.
You can download the test project I have used to test your code here.
这篇关于如何将包含列表的字典绑定到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文