WinForm - Localize DataGridView with automatical generated columns
-
Hi! I have a DataGridView in a Windows Form application where I want to localize the columns in the DataGridView. I use the DataGridView 's
DataSource
property to populate the DataGridView from a list. Therefore the columns in the DataGridView are genereated automatically after the properies in the list. Here is the code to populate the DataGridView.public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();var p1 = new Person() { Name = "Foo", Age = 1337 }; var p2 = new Person() { Name = "Bar", Age = 42 }; var people = new List() { p1, p2 }; dataGridView1.DataSource = people; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}The generated columns in the DataGridView are
Name
andAge
, the same as the properties in thePerson
class. I have set the Form'sLocalizable
property toTrue
. With aLabel
in the Form, I have verified that I can change the Label'sText
property to different values based on theCurrentCulture
/CurrentUICulture
I set. I switch between different cultures in theProgram.cs
file:static class Program
{
/// /// The main entry point for the application.
///
[STAThread]
static void Main()
{
var eng = "en";
var swe = "sv-SE";
var culture = eng;
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
}
My project have generated
Form1.resx
andForm1.sv.resx
files to handle both English and Swedish. In these files I can find the label's text for the English and Swedish languages accordingly. I just don't know how to set the DataGridView's column to different languages. Since the columns are not manually created by me I don't have the appropriate values in the Form1.resx and Form1.sv.resx to set the columns based on the selected language. Is there any solution to this? -
Hi! I have a DataGridView in a Windows Form application where I want to localize the columns in the DataGridView. I use the DataGridView 's
DataSource
property to populate the DataGridView from a list. Therefore the columns in the DataGridView are genereated automatically after the properies in the list. Here is the code to populate the DataGridView.public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();var p1 = new Person() { Name = "Foo", Age = 1337 }; var p2 = new Person() { Name = "Bar", Age = 42 }; var people = new List() { p1, p2 }; dataGridView1.DataSource = people; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}The generated columns in the DataGridView are
Name
andAge
, the same as the properties in thePerson
class. I have set the Form'sLocalizable
property toTrue
. With aLabel
in the Form, I have verified that I can change the Label'sText
property to different values based on theCurrentCulture
/CurrentUICulture
I set. I switch between different cultures in theProgram.cs
file:static class Program
{
/// /// The main entry point for the application.
///
[STAThread]
static void Main()
{
var eng = "en";
var swe = "sv-SE";
var culture = eng;
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
}
My project have generated
Form1.resx
andForm1.sv.resx
files to handle both English and Swedish. In these files I can find the label's text for the English and Swedish languages accordingly. I just don't know how to set the DataGridView's column to different languages. Since the columns are not manually created by me I don't have the appropriate values in the Form1.resx and Form1.sv.resx to set the columns based on the selected language. Is there any solution to this?Because of the way DGV columns are created, you are going to have to iterate over them to localize them: [^]. Look at this excerpt from Hans Passant's code in the link:
foreach (DataGridViewColumn col in dgv.Columns) {
resources.ApplyResources(col, col.Name);
}«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali