C# gridview 将下拉列表绑定到 List<keyvaluePair<int, string>>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/499526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gridview bind dropdownlist to List<keyvaluePair<int, string>>
提问by Jon
I have a number of tables in my DB that hold references to an key value pair:
我的数据库中有许多表保存对键值对的引用:
types of phone numbers:
电话号码类型:
- 1 - Home
- 2 - Work
- 3 - Mobile
- 4 - Fax
- 1 - 主页
- 2 - 工作
- 3 - 手机
- 4 - 传真
etc
等等
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.
所以我有一个类型表,当它们在其他表中使用时,它们引用 int 值作为外键。当我将它们拉出时,我一直将它们作为keyvaluepair<int, string>
项目存储在使用它们的类中。
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.
当我需要获取它们的列表时,我想我只需创建它们的 List<> 而不是使用两种不同类型的数据类型来获取相同的数据。
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.
当我使用 edittemplate 位时需要在 gridview 中填充下拉列表时,我的问题就出现了。如果我使用数据源将其提取出来,它将在文本中写入 [1 Home] 而不是将 int 作为值并将 Home 作为要显示的文本。
I guess I have a multiple part question really.
我想我真的有一个多部分的问题。
One:
一:
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.
我傻吗?这是获取数据并存储它(键值对部分)的一种非常糟糕的方式吗?我应该将它全部存储在数据表中吗?我不喜欢把它全部放在数据表中。我将我的 DAL 带到我的 BLL 并尝试将所有内容封装为对象或对象的List<>
's 而不是所有内容的表。大多数情况下,这运作良好。
Two:
二:
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?
如果我使用某个对象而不是数据表绑定到下拉列表的对象数据源中,我该如何设置当前选定的值,而不是只选择列表中的第一项?
EDIT
编辑
As was pointed out below I was being an idiot and just needed to set the DataValueField and DataKeyField.
正如下面指出的那样,我是个白痴,只需要设置 DataValueField 和 DataKeyField。
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.
我没有立即看到它的原因是因为它没有出现在我的智能感知中,但是当我手动输入它时,它起作用了。
采纳答案by CMS
Use a Dictionary<int, string> and set your DropDown DataValueField to Keyand DataTextField to Value.
使用 Dictionary<int, string> 并将 DropDown DataValueField 设置为Key并将 DataTextField 设置为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();
回答by mRizvandi
and its my custom method
它是我的自定义方法
// Define enum
public enum ServiceType : int
{
MinimumService = 1,
NormalService = 2,
VipService = 99
}
// custom method to get my custom text name for each enum type
public string GetServiceTypeName(ServiceType serviceType)
{
string retValue = "";
switch (serviceType)
{
case ServiceType.Print:
retValue = "We have some services for you";
break;
case ServiceType.BookBinding:
retValue = "We ar glad to meet you";
break;
default:
retValue = "We alywas are ready to make you happy";
break;
}
return retValue;
}
// making dictionary (key and value) for dropdown datasource
public static Dictionary<string, int> GetAllServiceTypeName()
{
Dictionary<string, int> dataSource = new Dictionary<string, int>();
foreach (int item in Enum.GetValues(typeof(ServiceType)))
dataSource.Add(GetServiceTypeName((ServiceType)item), item);
return dataSource;
}
// bind the dropdown to dictionary
ddlServiceType.DataSource = GetAllServiceTypeName();
ddlServiceType.DataBind();
// aspx markup code sample
<asp:DropDownList ID="ddlServiceType" runat="server"
DataTextField="Key" DataValueField="Value">
</asp:DropDownList>