Editable checkbox in a Grid with AllowCellSelection disabled

The GridHitTest method can be used to to determine where user clicked on the grid and update checkbox accordingly. Note that checkbox status can be changed by mouse only because there's no current cell when AllowCellSelection=.F.

This is sample code. Add error handling and adjust to your requirements as necessary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.Show
RETURN

DEFINE CLASS form1 AS form
	Top = 0
	Left = 0
	Height = 276
	Width = 504
	DoCreate = .T.
	Caption = "Form1"
	ngridx = 0
	ngridy = 0
	Name = "Form1"

	ADD OBJECT grid1 AS grid WITH ;
		ColumnCount = 3, ;
		HeaderHeight = 39, ;
		Height = 216, ;
		Left = 12, ;
		Panel = 1, ;
		Top = 24, ;
		Width = 456, ;
		AllowCellSelection = .F., ;
		Name = "Grid1", ;
		Column1.Width = 134, ;
		Column1.Name = "Column1", ;
		Column2.Width = 111, ;
		Column2.Name = "Column2", ;
		Column3.Width = 62, ;
		Column3.Sparse = .F., ;
		Column3.Name = "Column3"

	PROCEDURE grid1.Init
		WITH This.Column3
			.Header1.Caption = "Checkbox"
			.AddObject("Check1","CheckBox")
			.Sparse = .F.
			.CurrentControl = "Check1"
			WITH .Check1
				.Alignment = 2
				.Caption = ""
				.Name = "Check1"
				.Visible = .T.
			ENDWITH
			.RemoveObject("text1")
		ENDWITH

	ENDPROC

	PROCEDURE Load
		CREATE CURSOR Test ( t1 C(10), t2 C(10), l1 L)
		INSERT INTO Test VALUES ( "Test 1 ", "Str 1", .F.)
		INSERT INTO Test VALUES ( "Test 2 ", "Str 2", .T.)
		INSERT INTO Test VALUES ( "Test 3 ", "Str 3", .F.)
		INSERT INTO Test VALUES ( "Test 4 ", "Str 4", .T.)
		GO TOP
	ENDPROC

	PROCEDURE grid1.Click
		LOCAL lnRelCol, lnRelRow, lnWhere
		STORE 0 TO lnWhere, lnRelRow, lnRelCol
		This.GridHitTest(Thisform.nGridX, Thisform.nGridY, @lnWhere, @lnRelRow, @lnRelCol)
		IF lnWhere = 3			&& Cell
			IF lnRelCol = 3		&& column 3
				This.Columns(lnRelCol).check1.Value = NOT This.Columns(lnRelCol).check1.Value
			ENDIF
		ENDIF
	ENDPROC

	PROCEDURE grid1.MouseDown
		LPARAMETERS nButton, nShift, nXCoord, nYCoord
		* Save mouse position to use in Grid.Click
		Thisform.nGridX = nXCoord
		Thisform.nGridY = nYCoord
	ENDPROC

ENDDEFINE

Comments

good code,tanks!

very nice. congratulations...

Very good, thank you!