GridView控件绑定DropDownList中列出&LT; keyvaluePair&LT; INT,字符串&GT;&GT; [英] gridview bind dropdownlist to List<keyvaluePair<int, string>>
问题描述
我有一个数字,持有引用的键值对在我的数据库表:
I have a number of tables in my DB that hold references to an key value pair:
类型的电话号码的:
- 1 - 首页
- 2 - 工作
- 在3 - 移动
- 在4 - 传真
等
所以,我对类型的表格,当他们在其他表中使用它们引用的int值作为外键。当我已经拉出来,我一直将其存储为 keyvaluepair&LT; INT,字符串&GT;
正在使用他们在类项目
So I have a table for the types and when they are used in other tables they reference the int value as a foreign key. When I have been pulling them out I have been storing them as keyvaluepair<int, string>
items in the class that is using them.
当我需要得到他们的,我想我会只是创建一个列表℃的列表;>他们,而不是使用两种不同类型的数据类型,以获得相同的数据。
When I needed to get a list of them I thought I would just create a List<> of them rather than use two different types of data types to get the same data.
我的问题已经到了,当我需要的时候我用的是edittemplate位到GridView中填充一个DropDownList。如果我用一个数据源拉了这一点,它会写[1页]文本,而不是把INT的价值和家庭因为要显示的文本。
My problem has arrived when I need to populate a dropdownlist within a gridview when I am using the edittemplate bit. If I use a datasource to pull this out it will write [1 Home] in the text rather than put the int as the value and Home as the text to display.
我想我有多个部分的问题真的。
I guess I have a multiple part question really.
一:
我是不是愚蠢吗?这是一个非常糟糕的方式获取数据并存储它(keyvaluepair部分)?如果我只是将它存储所有的数据表?我不喜欢把它所有的数据表。我有我的DAL了结我的BLL和试图封装一切,对象或名单,其中;&GT;
的对象,而不是所有的表。大多数这种运作良好的时间。
Am I being stupid? Is this a really bad way to get the data out and store it (the keyvaluepair part)? Should I just be storing it all in a datatable? I didn't like to put it all in datatable. I have my DAL taking to my BLL and have tried to encapsulate everything as objects or List<>
's of objects rather than tables of everything. Most the time this has worked well.
二:
如果我使用一些对象,而不是一个DataTable绑定到我的ObjectDataSource的DropDownList的,我怎么可以设置当前选定的价值,而不是有它只是在列表中的第一个项目选择?
If I was using some object rather than a datatable to bind into my objectdatasource for the dropdownlist, how can I set the currently selected value, rather than have it just have the first item in the list selected?
修改
正如有人指出下面我被一个白痴,只是需要设置DataValueField和DataKeyField。
As was pointed out below I was being an idiot and just needed to set the DataValueField and DataKeyField.
要获得的DropDownList绑定我必须做的:
To get the dropdownlist to bind I just had to do:
SelectedValue='<%# DataBinder.Eval(Container, "DataItem.PhoneType.Key") %>'
究其原因,我没有看到一个马上是因为它没有出现在我的智能感知,但是当我手动键入它,它的工作。
The reason I didn't see that one straight away was because it was not showing up in my intellisense but when I manually typed it in, it worked.
推荐答案
使用字典&LT; INT,字符串&GT;和设置您的下拉DataValueField到的键的和DataTextField到的值的。
Use a Dictionary<int, string> and set your DropDown DataValueField to Key and DataTextField to Value.
// A sample dictionary:
var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "Home");
dictionary.Add(2, "Work");
dictionary.Add(3, "Mobile");
dictionary.Add(4, "Fax");
// Binding the dictionary to the DropDownList:
dropDown.DataTextField = "Value";
dropDown.DataValueField = "Key";
dropDown.DataSource = dictionary; //Dictionary<int, string>
dropDown.DataBind();
这篇关于GridView控件绑定DropDownList中列出&LT; keyvaluePair&LT; INT,字符串&GT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!