Search code examples
c#dynamicdatagridviewrefactoringdatagridviewcolumn

How to refactor the programmatically building of some DataGridViews columns?


I am trying to build some datagridviews. I am currently using the code shown below. Im wondering if there is a more efficient way to build the columns. I tried this method post, but cant seem to make it work. Does anyone have any suggestions?

        DataGridView hire = form1.hireDataGridView;
        hire.ColumnCount = 6;
        hire.Columns[0].Name = "Name";
        hire.Columns[1].Name = "Type";
        hire.Columns[2].Name = "Date";
        hire.Columns[3].Name = "Cost";
        hire.Columns[4].Name = "Start Date";
        hire.Columns[5].Name = "End Date";

        DataGridView service = form1.serviceDataGridView;
        service.ColumnCount = 5;
        service.Columns[0].Name = "Name";
        service.Columns[1].Name = "Type";
        service.Columns[2].Name = "Date";
        service.Columns[3].Name = "Cost";
        service.Columns[4].Name = "Description";

        DataGridView relocate = form1.relocationDataGridView;
        relocate.ColumnCount = 9;
        relocate.Columns[0].Name = "Name";
        relocate.Columns[1].Name = "Type";
        relocate.Columns[2].Name = "Date";
        relocate.Columns[3].Name = "Cost";
        relocate.Columns[4].Name = "dist";
        relocate.Columns[5].Name = "latA";
        relocate.Columns[6].Name = "longA";
        relocate.Columns[7].Name = "latB";
        relocate.Columns[8].Name = "longB";

Solution

  • You can create a dictionary having the names of the columns per grid:

    using System.Collections.Generic;
    
    private Dictionary<DataGridView, string[]> GridsSettings;
    
    private void InitializeGridsSettings()
    {
      if ( GridsSettings != null) return;
      GridsSettings = new Dictionary<DataGridView, string[]>
      {
        [hireDataGridView] = new string[]
        { "Name", "Type", "Date", "Cost", "Start Date", "End Date" },
    
        [serviceDataGridView] = new string[]
        { "Name", "Type", "Date", "Cost", "Description" },
    
        [relocationDataGridView] = new string[]
        { "Name", "Type", "Date", "Cost", "dist", "latA", "longA", "latB", "longB" },
      };
    }
    

    Then you can dynamically generate:

      InitializeGridsSettings(); // Called from constructor for example
    
      foreach ( var grid in GridsSettings)
      {
        grid.Key.ColumnCount = grid.Value.Length;
        int indexColumn = 0;
        foreach ( string name in grid.Value )
          grid.Key.Columns[indexColumn++].Name = name;
      }
    

    Also you can use a table with names and types per grid:

    GridsSettings = new Dictionary<DataGridView, List<(string, Type)>>
    {
      [hireDataGridView] = new List<(string, Type)>
      { ("Name", typeof(string)) },
    
      [serviceDataGridView] = new List<(string, Type)>
      { ("Name", typeof(string)) },
    
      [relocationDataGridView] = new List<(string, Type)>
      { ("Name", typeof(string)) },
    };
    
    foreach ( var grid in GridsSettings)
    {
      grid.Key.ColumnCount = grid.Value.Count;
      int indexColumn = 0;
      foreach ( var column in grid.Value )
      {
        grid.Key.Columns[indexColumn].Name = column.Item1;
        grid.Key.Columns[indexColumn].ValueType = column.Item2;
        indexColumn++;
      }
    }
    

    Data bindings can also be done the same manner to set database fields or object properties names, as well as anything suitable.