Sunday, 6 July 2008

Filling a DataGridView from a list of structs

An easy way I have found of filling a DataGridView with data, is when you configure the data source to be a list. As part of my Datalog project, I have a struct that represents a tracked machine set. Each new tracked set is added to a List<>. When the job run is complete, it is this List<> that I make the data source.

First, the code for the struct:

   1:  /// <summary>
   2:  /// Structure representing a tracked machine set.
   3:  /// </summary>
   4:  public struct TrackedSet
   5:  {
   6:      #region Fields
   7:   
   8:      private int _id;
   9:      private string _date;
  10:      private string _time;
  11:      private string _customer;
  12:      private int _sheets;
  13:      private char _insert1;
  14:      private char _insert2;
  15:      private char _insert3;
  16:      private char _insert4;
  17:      private char _insert5;
  18:      private char _insert6;
  19:      private char _insert7;
  20:      private char _insert8;
  21:      private char _insert9;
  22:      private char _insert10;
  23:      private char _sysCtrl;
  24:      private int _weight;
  25:      private string _error;
  26:   
  27:      #endregion
  28:   
  29:      #region Properties
  30:   
  31:      // The order that the properties are defined determines the order
  32:      // of columns created in the DataGridView.
  33:      // The column names will be taken from the property name, unless overwritten.
  34:   
  35:      public int ID
  36:      { get { return this._id; } }
  37:   
  38:      public string Date
  39:      { get { return this._date; } }
  40:   
  41:      public string Time
  42:      { get { return this._time; } }
  43:   
  44:      public string Customer
  45:      { get { return this._customer; } }
  46:   
  47:      public int Sheets
  48:      { get { return this._sheets; } }
  49:          
  50:      public char Insert1
  51:      { get { return this._insert1; } }
  52:   
  53:      public char Insert2
  54:      { get { return this._insert2; } }
  55:   
  56:      public char Insert3
  57:      { get { return this._insert3; } }
  58:   
  59:      public char Insert4
  60:      { get { return this._insert4; } }
  61:   
  62:      public char Insert5
  63:      { get { return this._insert5; } }
  64:   
  65:      public char Insert6
  66:      { get { return this._insert6; } }
  67:   
  68:      public char Insert7
  69:      { get { return this._insert7; } }
  70:   
  71:      public char Insert8
  72:      { get { return this._insert8; } }
  73:   
  74:      public char Insert9
  75:      { get { return this._insert9; } }
  76:   
  77:      public char Insert10
  78:      { get { return this._insert10; } }
  79:   
  80:      public char SysCtrl
  81:      { get { return this._sysCtrl; } }
  82:   
  83:      public int Weight
  84:      { get { return this._weight; } }
  85:   
  86:      public string Error
  87:      { get { return this._error; } }
  88:   
  89:      #endregion
  90:   
  91:      #region Constructor
  92:   
  93:      /// <summary>
  94:      /// TrackedSet constructor.
  95:      /// </summary>
  96:      /// <param name="id">The ID code for the tracked set</param>
  97:      /// <param name="msg">The serial link message string decoded from job data block</param>
  98:      /// <param name="created">The creation time of the tracked set</param>
  99:      /// <param name="weight">The post weight of the tracked set.</param>
 100:      /// <param name="error">The error code of the tracked set</param>
 101:      public TrackedSet(int id, DateTime created, string msg, int weight, string error)
 102:      {
 103:          // Get ID.
 104:          this._id = id;
 105:   
 106:          // Get created date.
 107:          this._date = created.ToShortDateString();
 108:   
 109:          // Get created time.
 110:          this._time = created.ToLongTimeString();
 111:   
 112:          // Get serial link message. Correct format : 12345678901,123,abcdefghij
 113:              
 114:          string[] str = msg.Split(',');
 115:   
 116:          // Get insert requests and system control characters.
 117:          char[] c = str[0].ToCharArray();
 118:   
 119:          this._insert1 = c[0];
 120:          this._insert2 = c[1];
 121:          this._insert3 = c[2];
 122:          this._insert4 = c[3];
 123:          this._insert5 = c[4];
 124:          this._insert6 = c[5];
 125:          this._insert7 = c[6];
 126:          this._insert8 = c[7];
 127:          this._insert9 = c[8];
 128:          this._insert10 = c[9];
 129:          this._sysCtrl = c[10];
 130:   
 131:          // Get sheet count.
 132:          bool result = int.TryParse(str[1], out this._sheets);
 133:   
 134:          // Get customer code.
 135:          this._customer = str[2];
 136:   
 137:          // Get post weight.
 138:          this._weight = weight;
 139:   
 140:          // Get error code.
 141:          this._error = error;
 142:      }
 143:  }


The list is defined as...

   1:  List<TrackedSet> _trackedSetList;


...and created as...

   1:  this._trackedSetList = new List<TrackedSet>();


Add new TrackedSets during a job run in this way:

   1:  // Add a new TrackedSet to the list.
   2:  this._trackedSetList.Add(new TrackedSet(this._rowCounter, 
   3:                                          DateTime.Now, 
   4:                                          this._serialLinkMessage, 
   5:                                          this._postWeightValue, 
   6:                                          this._errorCode));


Then it is simply a matter of setting the .DataSource property of the DataGridView to the list to enable it to be displayed:

   1:  this.dataGridView1.DataSource = this._trackedSetList;


This produces an output display similar to:



Click the image to see a larger view.



It is important to note thate the order that columns are created and displayed in the DataGridView is directly related to the order in which the struct properties as listed.

2 comments:

Almirante Coronel RAMMS said...

Excelente, tenia ese problema cuando hacia el binding de los datos no me mostraba nada, aunque tuviera datos, gracias a tu pagina, vi el error eran las propiedades...

Iceteh said...

Thank you so much. I have been looking for an example of binging a list of struct to datagridview.