View Issue Details

IDProjectCategoryView StatusLast Update
0036067LazarusLCLpublic2019-09-14 19:04
ReporterArlen B TaylorAssigned ToBart Broersma 
PrioritynormalSeverityminorReproducibilityalways
Status feedbackResolutionopen 
Platformx86_64 linuxOSKubuntuOS Version19.04
Product Version2.0.2Product Build61796 
Target VersionFixed in Version 
Summary0036067: Grids.pas shows error in TCustomStringGrid.InsertRowWithValues
DescriptionThis appears to be the same issue as raised in ID 0026943.

This concerns the TStringGrid function InsertRowWithValues [Grids.pas, lines 11681].

My function:

function TFormViewEditCharMatrix.AddCharToList(wnbChar: Integer;
  BoolRefreshPos: Boolean): Integer;
var
  Style1, Style2, Style3 : string;
  R : integer;
begin
  R := StrGridMatrix.RowCount + 1;

  StrGridMatrix.Columns.Enabled;

  if CharMatrix.ChMatrix.ChArr[wnbChar].Style and $01 = 1 then
    Style1 := '1' else Style1 := '0';

  if CharMatrix.ChMatrix.ChArr[wnbChar].Style and $02 = 2 then
    Style2 := '1' else Style2 := '0';

  if CharMatrix.ChMatrix.ChArr[wnbChar].Style and $04 = 4 then
    Style3 := '1' else Style3 := '0';

  StrGridMatrix.InsertRowWithValues(R,[Format(' %.5d', [wnbChar]),
    Style1, Style2, Style3]);
....
  Result := R;
end;

The error that comes up is: EGridException.Create('Use Columns property to add/remove columns');

No columns are being added, only a row.
Steps To ReproduceIn my program when I get to the line for InsertRowWithValues, it takes me to Grids.pas.

Incrementally going forward takes us to TCustomGrid.GetRowCount: which returns a result of 0, which is correct.

Then to : TCustomGrid.GetColCount: which returns 0, which is correct.

We are next taken to: SetColCount which takes us to GetColumns: TGridColumns which takes us to GetEnabled which returns 0.

Next we go to GetVisibleCount which returns 5, which is correct. Going back to GetEnabled returns true.

Finally we get to SetColCount(AValue) which shows 4 which if we are using 0 based counting is correct, but if 1 based like in GetVisibleCount should be 5.

After the error message shown, we go back to InsertRowWithValues, but no rows are shown in Form.

Additional Information
Grids.pas
[11681]
procedure TCustomStringGrid.InsertRowWithValues(Index: Integer;
  Values: array of String);
var
  i, OldRC: Integer;
begin
  OldRC := RowCount;
  if Length(Values) > ColCount then
    ColCount := Length(Values);
  InsertColRow(false, Index);
  //if RowCount was 0, then setting ColCount restores RowCount (from FGridPropBackup)
  //which is unwanted here, so reset it (Issue 0026943)
  if (OldRc = 0) then RowCount := 1;
  for i := 0 to Length(Values)-1 do
    Cells[i, Index] := Values[i];
end;

[2538]
function TCustomGrid.GetRowCount: Integer;
begin
  Result:=FRows.Count;
end;

[2533]
function TCustomGrid.GetColCount: Integer;
begin
  Result:=FCols.Count;
end;

[3031]
procedure TCustomGrid.SetColCount(AValue: Integer);
begin
  if Columns.Enabled then
    raise EGridException.Create('Use Columns property to add/remove columns');
  InternalSetColCount(AValue);
end;

[5536]
function TCustomGrid.GetColumns: TGridColumns;
begin
  result := FColumns;
end;

[12726]
function TGridColumns.GetEnabled: Boolean;
begin
  result := VisibleCount > 0;
end;

[12736]
function TGridColumns.GetVisibleCount: Integer;
{$ifNdef newcols}
var
  i: Integer;
{$endif}
begin
  {$ifdef newcols}
  result := Count;
  {$else}
  result := 0;
  for i:=0 to Count-1 do
    if Items[i].Visible then
      inc(result);
  {$endif}
end;

[]
procedure TCustomGrid.SetColCount(AValue: Integer);
begin
  if Columns.Enabled then
    raise EGridException.Create('Use Columns property to add/remove columns');
  InternalSetColCount(AValue);
end;
TagsNo tags attached.
Fixed in Revision
LazTarget-
WidgetsetGTK 2
Attached Files

Activities

Arlen B Taylor

2019-09-11 20:44

reporter  

LocalVariableValues.jpeg (41,163 bytes)
LocalVariableValues.jpeg (41,163 bytes)
StringGrid.jpeg (104,454 bytes)
StringGrid.jpeg (104,454 bytes)
StringGridErrorMsg.jpeg (38,374 bytes)
StringGridErrorMsg.jpeg (38,374 bytes)

Arlen B Taylor

2019-09-11 20:49

reporter   ~0118042

The old issue noted indicates that this was solved for Windows, but I am developing for Linux, so perhaps it only solved the problem for windows.

Arlen B Taylor

2019-09-11 21:07

reporter  

SetColCount.jpeg (18,952 bytes)
SetColCount.jpeg (18,952 bytes)
GetVisibleCount.jpeg (20,989 bytes)
GetVisibleCount.jpeg (20,989 bytes)
GetRowCount.jpeg (20,795 bytes)
GetRowCount.jpeg (20,795 bytes)

Bart Broersma

2019-09-11 22:36

developer   ~0118044

If you have Columns in a StringGrid, you can't use InsertRowsWithValues.
AFAIK it's by design.

Arlen B Taylor

2019-09-12 01:13

reporter   ~0118046

Really?!!

Here is code I used in another form in this application using InsertRowsWithValues


  FileListVideo := FileUtil.FindAllFiles(FilePath, SearchMask, true);

  if VobProcess.AutoSelectMatchingVobs = true then C := '1' else C := '0';

  R := 1;

  if LeftStr(SearchMask,8) <> 'VIDEO_TS' then
  begin
    if LeftStr(SearchMask,4) = 'VTS_' then
    begin
      for D := 0 to FileListVideo.Count -1 do
      begin
        if FindFirstUtf8(FilePath, faAnyFile and faDirectory, SearchRec1)=0 then
        begin
          repeat
            With SearchRec1 do
            begin
              FN := ExtractFileName(FileListVideo.Strings[D]);
              Ext := SysToUtf8(ExtractFileExt(FN));
              K := IntToStr(FileSizeUtf8(FileListVideo.Strings[D]));
              Case Ext of
                '.IFO' : StringGridVob.InsertRowWithValues(R,['0',FN, K]);
                '.VOB' : StringGridVob.InsertRowWithValues(R,[C,FN,K]);
                '.BUP' : Continue;
                '.srm' : Continue;
                '.SRM' : Continue;
              end;
              if RightStr(FileListVideo.Strings[D], 4) = '.IFO' then
                VobProcess.LoadIfo(FileListVideo.Strings[D]);
            inc(R, 1);
            end;
          until FindNextUtf8(SearchRec1) <> 0;
        end;
        FindCloseUtf8(SearchRec1);
      end;
    end;
  end;

FormVobProcess.jpeg (102,758 bytes)
FormVobProcess.jpeg (102,758 bytes)

Arlen B Taylor

2019-09-12 05:30

reporter   ~0118047

I seem to have gotten passed the problem by artificially making my rowcount + 2 on the first iteration, then rowcount + 1 thereafter. At least I am not getting that error message any more.

I appreciate your review of this issue.

Bart Broersma

2019-09-12 18:21

developer   ~0118051

Last edited: 2019-09-12 19:01

View 2 revisions

Do you use "custom columns" that is, did you use the Columns property to add columns, instead of setting ColCount for your grid?
IIRC when using the Columns property, you cannot set ColCount (which is by design).

Bart Broersma

2019-09-14 19:03

developer   ~0118079

@arlen: please test with attached patch "grids.insertrowwithvalues.diff".

grids.insertrowwithvalues.diff (851 bytes)
Index: lcl/grids.pas
===================================================================
--- lcl/grids.pas	(revision 61683)
+++ lcl/grids.pas	(working copy)
@@ -11749,11 +11749,19 @@
 procedure TCustomStringGrid.InsertRowWithValues(Index: Integer;
   Values: array of String);
 var
-  i, OldRC: Integer;
+  i, OldRC, Diff: Integer;
 begin
   OldRC := RowCount;
-  if Length(Values) > ColCount then
-    ColCount := Length(Values);
+  Diff := Length(Values) - ColCount;
+  if Diff > 0 then
+  begin
+    if Columns.Enabled then
+    begin
+      for i := 1 to Diff do with Columns.Add do Title.Caption := '';
+    end
+    else
+      ColCount := Length(Values);
+  end;
   InsertColRow(false, Index);
   //if RowCount was 0, then setting ColCount restores RowCount (from FGridPropBackup)
   //which is unwanted here, so reset it (Issue #0026943)

Issue History

Date Modified Username Field Change
2019-09-11 20:44 Arlen B Taylor New Issue
2019-09-11 20:44 Arlen B Taylor File Added: LocalVariableValues.jpeg
2019-09-11 20:44 Arlen B Taylor File Added: StringGrid.jpeg
2019-09-11 20:44 Arlen B Taylor File Added: StringGridErrorMsg.jpeg
2019-09-11 20:49 Arlen B Taylor Note Added: 0118042
2019-09-11 21:07 Arlen B Taylor File Added: SetColCount.jpeg
2019-09-11 21:07 Arlen B Taylor File Added: GetVisibleCount.jpeg
2019-09-11 21:07 Arlen B Taylor File Added: GetRowCount.jpeg
2019-09-11 22:36 Bart Broersma Note Added: 0118044
2019-09-12 01:13 Arlen B Taylor File Added: FormVobProcess.jpeg
2019-09-12 01:13 Arlen B Taylor Note Added: 0118046
2019-09-12 05:30 Arlen B Taylor Note Added: 0118047
2019-09-12 18:21 Bart Broersma Note Added: 0118051
2019-09-12 19:01 Bart Broersma Note Edited: 0118051 View Revisions
2019-09-13 18:01 Bart Broersma Status new => feedback
2019-09-13 18:01 Bart Broersma LazTarget => -
2019-09-14 19:03 Bart Broersma File Added: grids.insertrowwithvalues.diff
2019-09-14 19:03 Bart Broersma Note Added: 0118079
2019-09-14 19:04 Bart Broersma Assigned To => Bart Broersma
2019-09-14 19:04 Bart Broersma Status feedback => assigned
2019-09-14 19:04 Bart Broersma Status assigned => feedback