Skip to main content

Problem:

Using the DataGridView .Net control it is possible to have cells that contain a Push Button Control.

If you do this how can you detect when the button has been pressed ?

Resolution:

On the DataGridView you need to trap the CellContentClick event. This is generated when the button is pressed on the grid.

However this event is also generated on any other cell types as well. You therefore need to check that it is also a button cell type (DataGridViewButtonCell .Net type).

You can use the "instance of" operator in .Net COBOL to do this.

Some example code to trap a button click would be:-

       method-id.  "dataGridView1_CellContentClick" final private.

       01 ls-row   cDataGridViewRow.

       01 ls-cell  object.

       01 ls-cellbutton S-W-F-DataGridViewButtonCell.

       linkage section.

       01 sender object.

       01 e S-W-F-DataGridViewCellEventArgs.

       procedure division using by value  sender e.

       

      ***** Lets Trap Button Clicks.

       

      ***** First Get the Current Cell as System.Object

       

           set ls-row to DataGridView1::"Rows"::"Item"(e::"RowIndex")

           set ls-cell to ls-row::"Cells"::"Item"(e::"ColumnIndex")

           

      ***** Now see if its a Button Click based on Cell Type

       

           if ls-cell  is instance of S-W-F-DataGridViewButtonCell

               set ls-cellbutton to ls-cell as S-W-F-DataGridViewButtonCell

               invoke cMessageBox::"Show"(ls-cellbutton::"Value"::"ToString"())

           end-if

       

       end method "dataGridView1_CellContentClick".

Old KB# 1390