1

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";
Travis123
  • 25
  • 7
  • What is "more efficient" to you? The method on the post you mentioned seems to be valid. – Bruno Canettieri May 11 '21 at 00:48
  • Are you having any problems with your current method? If not, [why are you asking?](https://ericlippert.com/2012/12/17/performance-rant/) Note that the setter of `ColumnCount` [internally](https://source.dot.net/#System.Windows.Forms/System/Windows/Forms/DataGridView.cs,1517) calls `.Columns.Add()` just like the answer you referred to. – 41686d6564 stands w. Palestine May 11 '21 at 00:50
  • 1
    It'll be more *efficient* when you'll define the Data Type of each Column instead of leaving it *undetermined* as it is now. – Jimi May 11 '21 at 03:55

1 Answers1

0

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.