Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WinForm - Localize DataGridView with automatical generated columns

WinForm - Localize DataGridView with automatical generated columns

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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 and Age, the same as the properties in the Person class. I have set the Form's Localizable property to True. With a Label in the Form, I have verified that I can change the Label's Text property to different values based on the CurrentCulture / CurrentUICulture I set. I switch between different cultures in the Program.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 and Form1.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?

    B 1 Reply Last reply
    0
    • M Mc_Topaz

      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 and Age, the same as the properties in the Person class. I have set the Form's Localizable property to True. With a Label in the Form, I have verified that I can change the Label's Text property to different values based on the CurrentCulture / CurrentUICulture I set. I switch between different cultures in the Program.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 and Form1.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?

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups