Highlighted
Absent Member.
Absent Member.
2047 views

Select particular cell in Datagridview

Jump to solution

I am using Visual Cobol 2.2 -1  and Visual studio 2012
Problem :
How to set a particular datagridview cell active within handling a Datagridview event.
The datagridview is NOT BOUND to a dataset.

The Windows forms app contains several controls, one of which is the Datagridview.
The Datagridview consists of 5 cols in this case.
Loading the grid and validatiing / editing / redisplaying values entered  is working fine.


When the user enters data in the first column (or any other column of course) of a row , the program is using the
'CellEndEdit' event-method to test some values of several columns and process accordingly.

----------------------------------  code begin ---------------------------------------------------
*>
*> Method to test what to do
*> dg-view is the name of the datagrid               

       method-id dg-view_CellEndEdit final private.
       procedure division using by value sender as object e as type System.Windows.Forms.DataGridViewCellEventArgs.

*>Within the method i want to set the focus to
*>the fourth column on the same row for example.
*>I have searched MSDN and tried other searches but all suggestions do not seem to work :
*>example of a solution i have tried with the Datagrid property 'SelectionMode' set to 'CellSelect' using designer:

        
                 set hlp-row to dg-view::CurrentCellAddress::Y.
                 set hlp-col to dg-view::CurrentCellAddress::X.

*>               ..... Get data of the current cell and process according to hlp-col value .......
*>               ......For the first cell (hlp-col = 0) on the current row the following lines are coded
*>               ......just before leaving the method ............
    
                 if hlp-col = 0
                    invoke dg-view::ClearSelection
                    set dg-view::CurrentCell to dg-view::Rows[hlp-row]::Cells[hlp-col + 3]                     
                    set dg-view::CurrentCell::Selected to true
                 end-if
    end method.

----------------------------------  code end ---------------------------------------------------


effect:
    whe the user leaves the first cell on row 0 , the focus will be on the second column
    instead of the fourth.  I have verified that hlp-col = 0 by debugging.


0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

The following example appears to work.

This sets a flag and saves the current coordinates in the CellEndEdit event but actually changes the current column to the desired one in the Selection_Changed event instead.

Currently this simply sets the focus back to the same column that was just edited but it seems to correctly change it to whatever column you set current-col and current-row fields to.

       class-id testgridrow.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       01 current-row  binary-long.
       01 current-col  binary-long.
       01 flag-cell-edited condition-value.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id dataGridView1_CellEndEdit final private.
       procedure division using by value sender as object e as type System.Windows.Forms.DataGridViewCellEventArgs.
           
           set flag-cell-edited to True
           set current-col to e::ColumnIndex
           set current-row to e::RowIndex

       end method.

       method-id dataGridView1_SelectionChanged final private.
       procedure division using by value sender as object e as type System.EventArgs.
           if flag-cell-edited 
              set DataGridView1::CurrentCell to DataGridView1[current-col, current-row]
              set flag-cell-edited to False
           end-if
    
       end method.
      
       end class.

View solution in original post

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert

The following example appears to work.

This sets a flag and saves the current coordinates in the CellEndEdit event but actually changes the current column to the desired one in the Selection_Changed event instead.

Currently this simply sets the focus back to the same column that was just edited but it seems to correctly change it to whatever column you set current-col and current-row fields to.

       class-id testgridrow.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       01 current-row  binary-long.
       01 current-col  binary-long.
       01 flag-cell-edited condition-value.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id dataGridView1_CellEndEdit final private.
       procedure division using by value sender as object e as type System.Windows.Forms.DataGridViewCellEventArgs.
           
           set flag-cell-edited to True
           set current-col to e::ColumnIndex
           set current-row to e::RowIndex

       end method.

       method-id dataGridView1_SelectionChanged final private.
       procedure division using by value sender as object e as type System.EventArgs.
           if flag-cell-edited 
              set DataGridView1::CurrentCell to DataGridView1[current-col, current-row]
              set flag-cell-edited to False
           end-if
    
       end method.
      
       end class.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.