From my topic of listview, I'm trying to use TStringgrid.
procedure TForm1.formcreate;
begin
Createcolumns(fDataset)
end;
procedure Tform1.Createcolumns(aDs : TDataset);
var index : integer;
begin
sgrid.ColCount := fdataset.FieldDefs.Count + 1;
for index := 0 to aDs.FieldDefs.Count - 1 do
sgrid.Columns[index].Title.Caption := aDs.FieldDefs[index].DisplayName;
end;
This code should create a few columns in stringgrid and change the title.
But no columns are created (showmessages shows 0) and a error occured:
Project project1 raised exception class 'EListError' with message:
List index (0) out of bounds
[Break] [Continue]
But when the form is activated, the actual called columns are on screen.
Nice. This is working and fits also the width of a cell:
procedure Tform1.Creatcolumns(aDs : TDataset);
var index, index2 : integer;
Column : TGridColumn;
Value : string;
begin
for index := 0 to aDs.FieldDefs.Count - 1 do
begin
Value := '';
Column := TGridColumn.Create(SGrid.Columns);
Column.Title.Caption := aDs.FieldDefs[index].DisplayName;
if aDs.FieldDefs[index].DataType = ftstring then
begin
for index2 := 1 to aDs.FieldDefs[index].Size do
Value := Value + 'D';
Column.Width := sgrid.canvas.TextWidth(Value);
end;
end;
sgrid.ColWidths[0] := 15;
end;
The last line of code changes the width of the fixes column. The fixed column is not a part of TGridcolumns.
The easiest way to fill the cells is using procedure InsertRowWithValues(index, array of string).
This procedure doesn't look if a fixed row is displayed, so the first cell of the index row is used.
That's not correct. To get a workaound you have create a first empty array parameter:
procedure Tform1.FillGrid(aDs : TDataset);
var Game : TGame;
index : integer;
begin
index := 1;
for Game in fGames do
begin
sgrid.InsertRowWithValues(index,['',game.Name,game.TypeofGame,game.NumberofPlayers,game.Duration.ToString]);
index := index + 1;
end;
end;
If you use InsertRowWithValues with index = 0 and sequentially fill the rows a new fixed row will be created.
I think it's a bug.
Using Alternatecolor gives every even row another color. That's nice.
The only thing I can't get right is the selection bar of a cell when option dgRowselected is false. THe selectbar is like
a dit read retangle. Is it posible to get it filled like DBGrid?