如何本地化 DataTable 列名

发布于 2024-08-23 16:59:42 字数 1240 浏览 1 评论 0原文

这是我的数据表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2024-08-30 16:59:42

将 resx 文件添加到项目后,Visual Studio 会生成强类型类,允许您访问它们。例如,如果您将 Messages.resx 添加到项目中,将创建一个 Messages 静态类:

table.Columns.Add(Messages.UserId, typeof(int));

其中 UserId 是您添加的字符串资源。

更好的解决方案是本地化用户界面而不是数据表的列名称。下面是一个示例:

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var table = new System.Data.DataTable();
            table.Columns.Add("UserID", typeof(int));
            for (int i = 0; i < 10; i++)
            {
                table.Rows.Add(i);
            }
            grdTest.DataSource = table;
            grdTest.DataBind();
        }
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdTest" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="UserID" 
                    HeaderText="default value" 
                    meta:resourcekey="Grid" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

页面上有一个 GridView 绑定到具有单列的 DataTable。请注意 BoundField 上的 meta:resourcekey="Grid" 属性。现在,将特殊的 App_LocalResources 文件夹添加到您的 Web 应用程序中,并在其中添加一个名为 Default.aspx.resx 的资源文件(假设该网页名为 Default.aspx)。您可以在其中添加以下字符串资源:

Grid.HeaderText  |  Some value

标记中给出的 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 a Messages static class will be created:

table.Columns.Add(Messages.UserId, typeof(int));

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:

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var table = new System.Data.DataTable();
            table.Columns.Add("UserID", typeof(int));
            for (int i = 0; i < 10; i++)
            {
                table.Rows.Add(i);
            }
            grdTest.DataSource = table;
            grdTest.DataBind();
        }
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdTest" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="UserID" 
                    HeaderText="default value" 
                    meta:resourcekey="Grid" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

There's a GridView on the page bound to a DataTable with a single column. Notice the meta:resourcekey="Grid" attribute on the BoundField. Now add the special App_LocalResources folder to your web application and inside it a resource file named Default.aspx.resx assuming the web page is called Default.aspx. Inside you could add the following string resource:

Grid.HeaderText  |  Some value

The default value of the HeaderText given in the markup will be replaced with the one in the resource file provided it is present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文