Some basics on DataGrids??

I've been asked to investigate the transfer of a screen containing many similar values into a Datagrid object as provided for in Visual COBOL.

Can anyone point me in the direction of some basic 'how to' instruction on how to:

a) create datagrids

b) populate them with data coming in from a table

c) movement between cells in a datagrid

d) validate cell data (as entered by the user)

e) retrieve datagrid content and put back in table

Thanks very much.

P.S a Wiki on this topic would be welciome!

  • I assume that you are referring to a dataGridView control in Windows Forms, is that correct?

    The easiest way to deal with populating and retrieving data from a dataGridView control is through the use of binding.

    It is possible in Visual COBOL to use EXEC ADO syntax to fill and manipulate a DataSet object which contains SQL tables and then to bind these to the grid control using the dataSource property of the control.

    You can also use the .NET Framework SQL classes directly to bind Tables, DataSets, Lists, etc. to a dataGridView.

    There are a couple of articles in the Micro Focus knowledgebase that include some samples for binding to dataGridView controls that should get you started.


    Bind SQL data to dataGridView and update datasource with changes in Visual COBOL

    Bind results of a COBOL SQL cursor to a dataGridView control in Visual COBOL


  • Hi Chris,

    Actually I need info on using the dataGridView as a NON-BOUND object.

    We have data coming is as a table in the form...

    01 Table.

        03 Table Item Occurs 50.

              03 Table-Code Pic XXX.

              03 Table-Desc Pic X(50).

              03 Table-Value Pic S9(7)V99.

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

    Any links to notes on achieving this would be most helpful.
  • Verified Answer

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

    Here is a simple demo using your 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 your 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
          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
          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 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
                 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 method.
          end class.