如何本地化 DataTable 列名
这是我的数据表,
public static DataTable GetTableForApproval()
{
using (var connection = Utils.Database.GetConnection())
using (var command = new SqlCommand("SELECT [UserID], [Username], " +
"[Email], [Role], [Date] FROM [Users] WHERE [Role] = @role",
connection))
{
command.Parameters.AddWithValue("@role", "Waiting");
using (var reader = command.ExecuteReader())
{
var table = new DataTable();
table.Columns.Add("UserID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Email", typeof(string));
table.Columns.Add("Role", typeof(string));
table.Columns.Add("Registration date", typeof(DateTime));
if (reader != null)
{
while (reader.Read())
{
table.Rows.Add((int)reader["UserID"],
(string)reader["Username"], (string)reader["Email"],
(string)reader["Role"], (DateTime)reader["Date"]);
}
}
return table;
}
}
}
我想本地化列名称。你能告诉我该怎么做吗?我本地化了 .aspx 页面,但我不知道如何本地化 .cs 文件中的文本。
This is my datatable
public static DataTable GetTableForApproval()
{
using (var connection = Utils.Database.GetConnection())
using (var command = new SqlCommand("SELECT [UserID], [Username], " +
"[Email], [Role], [Date] FROM [Users] WHERE [Role] = @role",
connection))
{
command.Parameters.AddWithValue("@role", "Waiting");
using (var reader = command.ExecuteReader())
{
var table = new DataTable();
table.Columns.Add("UserID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Email", typeof(string));
table.Columns.Add("Role", typeof(string));
table.Columns.Add("Registration date", typeof(DateTime));
if (reader != null)
{
while (reader.Read())
{
table.Rows.Add((int)reader["UserID"],
(string)reader["Username"], (string)reader["Email"],
(string)reader["Role"], (DateTime)reader["Date"]);
}
}
return table;
}
}
}
I want to localize the columns names. Can you tell me how can I do this? I localized my .aspx pages, but I don't know how to localize the text in .cs files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 resx 文件添加到项目后,Visual Studio 会生成强类型类,允许您访问它们。例如,如果您将
Messages.resx
添加到项目中,将创建一个Messages
静态类:其中
UserId
是您添加的字符串资源。更好的解决方案是本地化用户界面而不是数据表的列名称。下面是一个示例:
页面上有一个
GridView
绑定到具有单列的DataTable
。请注意 BoundField 上的meta:resourcekey="Grid"
属性。现在,将特殊的 App_LocalResources 文件夹添加到您的 Web 应用程序中,并在其中添加一个名为Default.aspx.resx
的资源文件(假设该网页名为Default.aspx
)。您可以在其中添加以下字符串资源:标记中给出的 HeaderText 的默认值将替换为资源文件中存在的值。
Once you have the resx files added to your project, Visual Studio generates strongly typed classes allowing you to access them. For example if you add the
Messages.resx
to your project aMessages
static class will be created:where
UserId
is a string resource you added.A better solution would be to localize the user interface and not the column names of the DataTable. Here's an example:
There's a
GridView
on the page bound to aDataTable
with a single column. Notice themeta:resourcekey="Grid"
attribute on the BoundField. Now add the special App_LocalResources folder to your web application and inside it a resource file namedDefault.aspx.resx
assuming the web page is calledDefault.aspx
. Inside you could add the following string resource:The default value of the
HeaderText
given in the markup will be replaced with the one in the resource file provided it is present.