我有一个与数据库表“ countries ”绑定的下拉列表,
该下拉列表将列countryname作为其datatextfield,将countrycode作为datavaluefield。
假设此表有3个条目,分别具有如下所示的列值。
印度,在
德国,DE
美国,美国
我将文化更改为de-DE,
因为我知道对单个静态文本使用resx文件。现在,如何将多个数据文本字段项同时本地化为de-DE?
为此,您需要将表行集合与值中资源文件中的值连接起来,然后将数据打包到将绑定到的自定义类型中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();
现在,将您的绑定属性更改DropDownList
为:
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
并绑定数据源:
ddl.DataSource = data;
编辑
为了在不使用LINQ的情况下获得相同的结果,您需要将资源加载到字典中,然后在counties表上进行迭代以构建数据源。像这样的东西:
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,一切就绪!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句