Created On:  01/30/2013

Problem:

Customer has a Windows Forms application that uses a dataGridView control.
They need to know how they can load the dataGridView control with data directly from a COBOL data table (OCCURS).
They do not want to use Data Binding but would like to directly load the table with data and be able to read the data back into the COBOL table after editing.

The data is in a COBOL table that looks like the following:
01 my-Table.
     03 my-Items occurs 10.
          05 Table-Code pic x(3).
          05 Table-Desc pic x(20).
          05 Table-Value pic S9(7)V99.

Essentially what they need to do is transfer the content of the table to the DataGridView object, edit the data, validate it & finally return DataGridView values back to the table elements.

How can this be done?

Resolution:

You can certainly populate the dataGridView control directly using its Rows and Cells collections.

Here is a simple demo using the COBOL table to load the grid when button1 is clicked and then to read the data from the grid after button2 is pressed and repopulate ther table:

class-id testdatagrid.Form1 is partial
    inherits type System.Windows.Forms.Form.
working-storage section.
01 my-Table.
     03 my-Items occurs 10.
          05 Table-Code pic x(3).
          05 Table-Desc pic x(20).
          05 Table-Value pic S9(7)V99.
01 num-items binary-long.
01 sub-1 binary-long.
01 table-value-decimal decimal.

method-id NEW.
procedure division.
     invoke self::InitializeComponent
     invoke self::loadTable
     goback.
end method.

method-id loadTable public.
procedure division.
     move "111" to table-code(1)
     move "Table Item 1" to table-desc(1)
     move 1234.50 to table-value(1)
     move "222" to table-code(2)
     move "Table Item 2" to table-desc(2)
     move -9876.99 to table-value(2)
     move "333" to table-code(3)
     move "Table Item 3" to table-desc(3)
     move 1324.00 to table-value(3)
     move "444" to table-code(4)
     move "Table Item 4" to table-desc(4)
     move 1234.50 to table-value(4)
     move "555" to table-code(5)
     move "Table Item 5" to table-desc(5)
     move -123.75 to table-value(5)
     move 5 to num-items
     goback.
end method.

method-id button1_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
     perform varying sub-1 from 1 by 1
          until sub-1 > num-items
          invoke dataGridView1::Rows::Add
          set dataGridView1::Rows[sub-1 - 1]::Cells["CodeNum"]::Value to table-code(sub-1)
          set dataGridView1::Rows[sub-1 - 1]::Cells["Description"]::Value to table-desc(sub-1)
          move table-value(sub-1) to table-value-decimal
          set dataGridView1::Rows[sub-1 - 1]::Cells["Amount"]::Value to table-value-decimal
     end-perform
end method.

method-id button2_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
     move 1 to sub-1
     perform varying myRow as type DataGridViewRow thru dataGridView1::Rows
          if myRow::Cells["CodeNum"]::Value = null
               exit perform
         end-if
         move myRow::Cells["CodeNum"]::Value to table-code(sub-1)
         move myRow::Cells["Description"]::Value to table-desc(sub-1)
         set table-value(sub-1) to type System.Convert::ToDecimal(myRow::Cells["Amount"]::Value)
         add 1 to sub-1
      end-perform
end method.
end class.