要做到这一点,您需要将表行集合与资源文件中的值连接在一起,然后将数据打包到一个自定义类型中
DropDownList
。因此:
var tableRows = table.Rows.Cast<DataRow>();
var resourceStrings = YourResourceClass.ResourceManager
.GetResourceSet(culture: CultureInfo.CurrentUICulture,
createIfNotExists: false,
tryParents: true)
.Cast<DictionaryEntry>();
var data = tableRows
.Join(resourceStrings,
row => row["countrycode"].ToString(),
resource => resource.Key,
(row, resource) => new KeyValuePair<string, string>(
row["countrycode"].ToString(),
resource.Value.ToString()))
.ToArray();
现在,更改
下拉框
至:
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
并绑定数据源:
ddl.DataSource = data;
编辑
为了在不使用LINQ的情况下实现相同的结果,您需要将资源加载到字典中,然后遍历countries表以构建数据源;类似于:
var resources = new Dictionary<string,string>();
var resourceSet = YourResourceClass.ResourceManager.GetResourceSet(
CultureInfo.CurrentUICulture, false, true);
foreach(DictionaryEntry kvp in resourceSet)
{
resources[kvp.Key] = kvp.Value.ToString();
}
var dataSource = new List<KeyValuePair<string, string>>();
foreach(DataRow in table.Rows)
{
var countryCode = row["countrycode"].ToString();
if(resources.ContainsKey(countryCode))
{
dataSource.Add(new KeyValuePair<string, string>(
countryCode,
resources[contryCode]));
}
}
接下来,绑定
dataSource
到你的DropDownList,你就准备好了!