Thursday, November 26, 2026

Editing check boxes in a DBGrid - revised

A year and a half ago, I wrote about editing check boxes in a DBGrid1; this was in 'the good old days' of Delphi 7 and dbExpress components. I had occasion to revisit a unit that used this code and found that it didn't work with Delphi 12CE and FireDAC, primarily because the grid was buffered with a memtable. After working with CoPilot on a solution and then working alone to improve that solution, I now present a new approach to editing check boxes in a DBGrid. Apart from anything else, this code also prevents the 0 and 1 that would appear using the old solution.

First, two clarifications to the previous code: there is no need to define columns in the grid, and the dgEditing flag in the grid's options should not be set. Thus there's no possibility of changing a value in a text field. The check box's value is changed by simply clicking on the check box: this is handled in the OnCellClick method of the grid. The check at the beginning if sametext (fn, 'finance') prevents anything bad happening if the user clicks on a field that is not drawn as a checkbox. The sametext function fixes any problems that might occur if one parameter is in upper case and another is in lower case (or in CamelCode format). 

procedure TShowContact2Cust.DBGrid1CellClick(Column: TColumn); var fn: string; begin fn:= DBGrid1.SelectedField.FieldName; if sametext (fn, 'finance') or sametext (fn, 'report') or sametext (fn, 'miyun') or sametext (fn, 'call') then begin ToggleCheckBox (dbgrid1.selectedfield); qUpdate.macrobyname ('fieldname').asraw:= fn; qUpdate.params[0].asinteger:= mtContact2CustID.asinteger; qUpdate.execsql; dbGrid1.EditorMode:= false; end; end;

qUpdate is required in this form as the grid's dataset is a memtable; in another form there is no memtable sitting between the database and the grid and so qUpdate is not required there. This query uses a macro, thus enabling it to update any one of the four fields without having to manipulate the query's text.

update contact2cust set !fieldname = 1 - !fieldname where id = :p1;

The ToggleCheckBox procedure is very simple and uses a trick that avoids having to test whether the parameter belongs to a memtable or a query.

Procedure ToggleCheckBox (sf: tfield); begin sf.DataSet.Edit; sf.AsInteger:= 1 - sf.AsInteger; sf.DataSet.Post; end;

And that's all the code that is necessary. Much simpler than what went before.

Internal links
[1] 1935



This day in blog history:

Blog # Date Title Tags
1443 26/11/2021 Opening Excel from a thread and displaying a file Programming, Delphi, Office automation, Threads
1690 26/11/2023 Guitar thoughts Guitars
1866 26/11/2024 Delphi logging Programming, Delphi

Saturday, November 21, 2026

Magnesium glycinate/CPAP

First of all, an update regarding the coronovirus1: apart from the unpleasant 12 hours at the beginning of this infection, the following few days were symptom free although I was relatively weak, gaining strength day by day. By Thursday, I felt that I was back to normal.

That day (12 November) was marked by two other health events: a new CPAP mask arrived and I started taking the magnesium glycinate supplement that is supposed to support sleep, relaxation, and overall magnesium intake. In terms of falling asleep and maintaining sleep, I think that this form of magnesium (as opposed to citrate or oxide) made a welcome contribution.

On the other hand, I was dismayed to see the CPAP readings for the past week. After almost no apneic events after I started with the new mask, the past five days have shown higher numbers of apneic events. It may be that the relaxing effect of magnesium glycinate is affecting the part of the brain that sends signals to breathe - central sleep apnea means that occasionally signals aren't sent, and the glycinate appears to make this worse. AI suggests that I stop taking the glycinate for a week to see whether the number of apneas decrease. I can go back to magnesium citrate without problem.

The virus was good for my weight: I dropped a kilo during the 'virus week' but most of that kilo has returned to my weight. I am worried about my excess weight - I can't seem to get rid of it. I tried to make an appoinment with a nutrionist (on the recommendation of my GP) but the earliest appointment that I could find was in February and in a city about 20 km away. This is not very good. In the mean time, I've decided to drop the Greek yoghurt from my breakfast to see whether this makes any difference. I don't like it very much so not eating it doesn't bother me. The yoghurt has 10% fat content that in some circumstances is supposed to be good but is also contributing calories.

I am even considering asking for the weight loss injections as lifestyle interventions over the past few years have not helped. I have a return appointment with my GP next Thursday which is when I will mention this. Without going into the biochemistry, these injections work by slowing stomach emptying that causes mild nausea which deters people from eating. I had mild nausea all through the 'virus week', causing me to reduce my eating, and indeed last Friday, when all the family came for dinner, I didn't eat anything due to nausea and a stomach ache that may have been caused by the virus.

When not thinking about my weight, I am assiduously practising playing the saxophone. My teacher tells me to blow harder to improve my tone and intonation. I think that I am definitely improving. I watched an interesting video a few days ago, "How long does it take to learn saxophone": it will take a few years to get to the level that I want to achieve, so I'm doing fine for the short period that I've been playing.

Whilst doodling, I started playing Ravel's "Bolero" (this just popped out): I had most of the first line but then I needed to reach a note that was below the lowest note of the saxophone; I had been playing in G and needed a B below this. Walking home after my lesson, I remembered that the Bolero is in C, so I could download the sheet music for the flute part and be able to play this without missing any notes. I haven't been doing this too seriously but I note that I have difficulty moving from D in the upper octave down to C and then B: the fingerings of B and C continue to confuse me at times as B is fingered 'higher' than C even though the note is lower. 

Internal links
[1] 2169



This day in blog history:

Blog #Date TitleTags
2021/11/2005Hello again (Kate Bush)Kate Bush
30721/11/2010Improving the dual list box dialogProgramming, SQL, Dual list box
109121/11/2017Joe CoolGrandfather
127521/11/2019Thesis updateDBA, Health
155521/11/2022Using the steam basketCooking