本地化Windows窗体并在运行时更改语言

时间:2012-10-15 14:29:35

标签: c# winforms localization resources

我正在关注此链接http://www.dotnetcurry.com/ShowArticle.aspx?ID=174以本地化Windows窗体并在运行时更改语言。标签/按钮的一切正常:文本框,但Datagridview它不起作用。当我检查资源文件时,法语版本和默认版本文本在那里,但是当我执行它时,最后一个版本正在加载..当我从列表框中选择时,它不会显示默认的英文版本。

如何使用上面链接中给出的机制在运行时更新datagridview头文本? enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

该代码只能找到添加到窗体的Controls集合中的控件。但是没有像这样添加DataGridView列,它会被添加到DataGridView控件中。您需要改进发布的代码,以便它还迭代它找到的任何DGV的列。像这样:

    private void ChangeLanguage(string lang) {
        var ci = new CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        foreach (Control c in this.Controls) {
            ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
            resources.ApplyResources(c, c.Name, ci);
            if (c.GetType() == typeof(DataGridView)) {
                var dgv = (DataGridView)c;
                foreach (DataGridViewColumn col in dgv.Columns) {
                    resources.ApplyResources(col, col.Name);
                }
            }
        }
    }
相关问题