This course teaches how to install and configure Windows Server 2012 R2. It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

I have a data entry form that users need to enter in their Approx. Time Spent (in minutes). For example, they work on an item for 10 minutes, I want them to just have to enter in "10" but if they worked on something for an hour and a half, I want them to enter "90". The data entered into this form I will be showing on a grid and I want the grid to compute that the total for those 2 items, "10" minutes and "90" minutes, was a total of 2 Hours and 40 minutes.

Any help is appreciated!

Any help is appreciated!

Please try my code...

```
function MinutesToHours(Minutes: Integer): string;
var
Hours: Integer;
Minutes: Integer;
begin
if (Minutes > 0) then begin
Hours := Minutes div 60;
Minutes := Minutes mod 60;
end
else begin
Hours := 0;
Minutes := 0;
end;
Result := Format('%.2d:%.2d', [Hours, Minutes]);
end;
```

Here are the codes

```
Method 1 - Get from database
procedure TForm1.Button1Click(Sender: TObject);
//Edit1.Text has the user name
var
Q: TADOQuery;
Total: Integer;
begin
Q := TADOQuery.Create(nil);
try
Q.Connection := ADOConnection1;
Q.SQL.Add(Format('SELECT SUM([Time Spent]) CNT FROM TABLE1 WHERE USER = %s', [Edit1.Text]));
Q.Active := True;
Total := Q.FieldByName('CNT').Asinteger;
Q.Active := False;
finally
FreeAndNil(Q);
end;
Label1.Caption := IntToStr(Total DIV 60) + ' Hrs ' + IntToStr(Total MOD 60) + ' Mins';
end;
Method 2 - loop through Dataset
procedure TForm1.Button2Click(Sender: TObject);
var
Total: Integer;
begin
Total := 0;
ADOQuery1.DisableControls;
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
Total := Total + ADOQuery1.FieldByName('[Time Spent]').AsInteger;
ADOQuery1.Next;
end;
ADOQuery1.DisableControls;
Label1.Caption := IntToStr(Total DIV 60) + ' Hrs ' + IntToStr(Total MOD 60) + ' Mins';
end;
```

```
procedure TForm1.AdoTable1CalcFields(DataSet: TDataSet);
begin
AdoTable1.FieldByName('Time').AsString := (inttostr(AdoTable1.FieldByName('TotalTime').Asinteger div 60) +' hours, '+ inttostr(AdoTable1.FieldByName('TotalTime').Asinteger mod 60) +' minutes');
end;
```

BTW:

10 minutes and 90 minutes =

Thanks!

```
procedure TForm1.AdoTable1CalcFields(DataSet: TDataSet);
begin
AdoTable1.FieldByName('Time').AsString := (inttostr(AdoTable1.FieldByName('TimeSpent').Asinteger div 60) +' hours, '+ inttostr(AdoTable1.FieldByName('TimeSpent').Asinteger mod 60) +' minutes');
end;
```

CalcField.JPG
Got to the DataController property of the TableView, expand it and you can create summary fields from there.

Select the column and field name then specify Sum as the calculation

Summary.PNG

SummarySetupField.bmp

```
with (AView as TcxGridDBTableView) do
with DataController.Summary do
begin
BeginUpdate;
try
with FooterSummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := Columns[2]; //index of the column you are summing
Kind := skSum;
end;
finally
EndUpdate;
end;
end;
```

```
procedure TForm1.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: String);
begin
if not VarIsNull(AValue) then
AText := Format('%d: %d', [AValue DIV 60, AValue MOD 60]);
end;
```

Just make sure you are using the GetText event of the summary field and not of the column

```
procedure TForm2.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryDefaultGroupSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: string);
begin
if not VarIsNull(AValue) then
AText := Format('%d:%d', [AValue DIV 60, AValue MOD 60]);
end;
or
procedure TForm1.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: String);
begin
if not VarIsNull(AValue) then
AText := Format('%d %d', [AValue DIV 60, AValue MOD 60]);
end;
```

Summary.png
Are you kidding?

I just gave you the correct answer, how does adding a calculated field to each row give you summary

Yes I did,

The accepted solution makes no sense, where as the Author is asking about formating the summary field and I quote "grid to compute that the total for those 2 items"

Creating a calculated field (as accepted here) works on each row but produces no summary. to get the summary, you actually have to create a summary field for the grid.

I use devexpress grid all the time and I know that for sure, So I just wondered how that could have been the correct solution.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Open in new window