绑定下拉列表与字典 [英] Bind drop down list with Dictionary

查看:300
本文介绍了绑定下拉列表与字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定字典到下拉列表中。

I'm binding a Dictionary to a drop down list.

比方说我在字典中的以下项目:

Say for example I have the following items in dictionary:

{"Test1", 123}, {"Test2", 321} 

我想下拉文本采用以下格式:

I'd like the drop down text to take the following format:

Test1 - Count: 123
Test2 - Count: 321

我正沿着以下路径要与没有运气:

I was going along the following path with no luck:

MyDropDown.DataTextFormatString = string.Format("{0} - Count: {1}", Key, Value);

感谢您:)

推荐答案

您可以通过在你的字典使用LINQ创建投影视图,并创建一个匿名类型来保存您的自定义格式。

You could create a projection view by using LINQ on your dictionary and create an anonymous type to hold your custom formatting.

Dictionary<string, int> values = new Dictionary<string, int>();
values.Add("First", 321);
values.Add("Second", 456);
values.Add("Third", 908);


var displayView = from display in values
                    select new { DisplayText = display.Key + " Count: " + display.Value };

DropDownList1.DataSource = displayView;
DropDownList1.DataTextField = "DisplayText";
DropDownList1.DataBind();

这篇关于绑定下拉列表与字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆