Tips_gui   >   Datagrids   >   Data Grids (All Contents)

Data Grids

Data grids are handy for quickly showing all the columns and rows which are in a data list.

The downside on the data grid is that the data list definition must be exactly defined as you want to display the data. (The order and names of columns.)

Mouseover Data Grid Cell

I ran into a situation where I wanted to find out the cell in a data grid object which the user was right-clicking over. The On evOpenContextMenu event occurs before any evCell... events occur. Using mouseover(kMLine) worked for figuring out the vertical cell, but using mouseover(kMHorzCell) did not work.

I emailed a demo to Tech Support asking why mouseover(kMHorzCell) would not work. They noted the problem and logged it as an enhancement request. (I am using v4.2.0.3 at the time of writing this tip.)

The solution was to use mouseover(kMHorz) to find the horizontal pixel position of the mouse in the window instance, and then figure out the data grid cell by working from left to right across the window and the data grid cells. The following code (with excessive comments) was used to figure out the vertical and horizontal cell position of the mouse over the data grid.

On evOpenContextMenu

; Finding the line number in the data list is easy.
; Remember to add the header row ($fixedrow) if it is visible.
Calculate VCellNum as mouseover(kMLine)+irGrid.$fixedrow

; Finding the column number is not so easy.
; Using mouseover(kMHorzCell) does not work for the data grid. (v4.2)
Calculate HCellNum as mouseover(kMHorzCell)

; Get the horizontal position of the mouse in the window instance.
Calculate HMouse as mouseover(kMHorz)

; Find out how many columns are scrolled over.
Calculate HScroll as irGrid.$hscroll

; Get the CSV string of column widths.
Calculate ColWidths as irGrid.$columnwidths

; Start at the left edge of the data grid.
Calculate HPosn as irGrid.$left
Calculate HCellNum as 0

; Loop through the columns till we get to the mouseover position.
While HPosn<HMouse
   
   Calculate HCellNum as HCellNum+1
   Calculate CellWidth as strtok('ColWidths',',')
   
   ; Only add the cell width if the column is visible.
   If HCellNum>HScroll
      Calculate HPosn as HPosn+CellWidth
   End If
   
End While

; Set the current cell to be the one we just right-clicked over.
Do irGrid.$gridvcell.$assign(VCellNum)
Do irGrid.$gridhcell.$assign(HCellNum)

Click the Run Demo button in the StudioTips Browser to test the code.

Move Cursor to New Data Grid Line

Adding a new line to a data grid is no problem.

Moving the cursor (the focus) to the newly added line in the data grid can be a challenge!

You can change the current cell in a data grid to another line by assigning the $gridvcell property.

There are 2 tricks you need to know in order to move the cursor (focus) to the new line.

  1. After adding the new line to the data list, you need to redraw the data grid so that the data grid will have the new line. If you try to assign the $gridvcell to the last line before a redraw the data grid object doesn't know about the new line yet.
  2. If the data grid's $fixedrow property is kTrue (show the column names header), then the $gridvcell value will be the data list line +1. If the $fixedrow property is kFalse, the $gridvcell property will be equal to the data list line.

The following code is used behind the Add Line button in the demo for this tip.

; Add a line to the data list and make it the current line.
Do iList.$add() Returns rLine
Do iList.$line.$assign(rLine.$line)

; Redraw the data grid object. (If we don't redraw, setting the vertical cell won't work.)
Do irDataGrid.$redraw()

; The VertCellNum is affected by whether or not the $fixedrow is visible or not.
; If the $fixedrow attribute is true, add 1 to the current data list line.
Calculate VertCellNum as rLine.$line+irDataGrid.$fixedrow

; Set the $gridvcell
Do irDataGrid.$gridvcell.$assign(VertCellNum)

; Set the horizontal cell to column 1.
Do irDataGrid.$gridhcell.$assign(1)

; Another redraw is not required.
Quit method kTrue