Ignore:
Timestamp:
Sep 10, 2022, 10:54:56 PM (2 years ago)
Author:
chronos
Message:
  • Fixed: Load table fields in records list and record edit form.
  • Fixed: Record add needs to insert new row into database.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormRecords.pas

    r28 r29  
    6060    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
    6161  Caption := STable + ' - ' + Table.Caption;
     62  Table.Fields.Load;
    6263  ReloadList;
    6364end;
     
    6869begin
    6970  if (Item.Index >= 0) and (Item.Index < Table.Records.Count) then
    70   with TRecord(Table.Records[Item.Index]) do begin
     71  with Table.Records[Item.Index] do begin
    7172    for I := 0 to Table.Fields.Count - 1 do begin
    72       if I = 0 then Item.Caption := TValue(Values[0]).GetString
    73         else Item.SubItems.Add(TValue(Values[I]).GetString);
     73      if I = 0 then Item.Caption := Values[0].GetString
     74        else Item.SubItems.Add(Values[I].GetString);
    7475    end;
    7576    Item.Data := Table.Records[Item.Index];
     
    116117  I: Integer;
    117118  NewValue: TValue;
     119  Columns: string;
     120  Values: string;
    118121begin
    119122  NewRecord := TRecord.Create;
    120   NewRecord.Parent := Table;
     123  NewRecord.Table := Table;
    121124  NewRecord.Values.Count := Table.Fields.Count;
    122125  for I := 0 to Table.Fields.Count - 1 do begin
    123     ValueClass := TField(Table.Fields[I]).GetValueClass;
     126    ValueClass := Table.Fields[I].GetValueClass;
    124127    NewValue := ValueClass.Create;
    125128    NewRecord.Values[I] := NewValue;
     
    132135    if ShowModal = mrOk then begin
    133136      Save(NewRecord);
    134       Table.Records.Add(NewRecord);
     137
     138      Values := '';
     139      Columns := '';
     140      for I := 0 to Table.Fields.Count - 1 do begin
     141        Columns := Columns + Table.Fields[I].Name;
     142        Values := Values + '"' + NewRecord.Values[I].GetString + '"';
     143        if I < Table.Fields.Count - 1 then begin
     144          Columns := Columns + ' , ';
     145          Values := Values + ' , ';
     146        end;
     147      end;
     148      Table.DbClient.Query('INSERT INTO ' + Table.Name + ' ( ' + Columns + ' )' +
     149        ' VALUES ( ' + Values + ' )');
     150
     151      NewRecord.Free;
    135152      ReloadList;
    136153    end else NewRecord.Free;
     
    150167  I: Integer;
    151168begin
    152   Table.LoadRecords;
     169  Table.Records.Load;
    153170
    154171  ListViewRecords.Columns.BeginUpdate;
    155   while ListViewRecords.Columns.Count > Table.Fields.Count do
    156     ListViewRecords.Columns[ListViewRecords.ColumnCount - 1].Free;
    157   while ListViewRecords.Columns.Count < Table.Fields.Count do
    158     ListViewRecords.Columns.Add;
    159   for I := 0 to Table.Fields.Count - 1 do begin
    160     ListViewRecords.Columns[I].Caption := TField(Table.Fields[I]).TextBefore;
    161     ListViewRecords.Columns[I].Width := 200;
     172  try
     173    while ListViewRecords.Columns.Count > Table.Fields.Count do
     174      ListViewRecords.Columns[ListViewRecords.ColumnCount - 1].Free;
     175    while ListViewRecords.Columns.Count < Table.Fields.Count do
     176      ListViewRecords.Columns.Add;
     177    for I := 0 to Table.Fields.Count - 1 do begin
     178      ListViewRecords.Columns[I].Caption := Table.Fields[I].TextBefore;
     179      ListViewRecords.Columns[I].Width := 200;
     180    end;
     181  finally
     182    ListViewRecords.Columns.EndUpdate;
    162183  end;
    163   ListViewRecords.Columns.EndUpdate;
    164184
    165185  ListViewRecords.Items.Count := Table.Records.Count;
Note: See TracChangeset for help on using the changeset viewer.