Databases

Multi-selection in DBGrid

Posted in Databases

Set dgMultiSelect option of DBGrid to true and put next code to the Button.OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var
  X: Word;
  TempBookmark: TBookMark;
begin
  with DBGrid1.DataSource.DataSet do
  begin
    DisableControls;
    with DBGrid1.SelectedRows do
      if Count<>0 then
      begin
        TempBookmark:=GetBookmark;
        for X:=0 to Count-1 do
        begin
          if IndexOf(Items[X])>-1 then
          begin
            Bookmark:=Items[X];
            ShowMessage(Fields[1].AsString);
          end;
        end;
      end;
    GotoBookmark(TempBookmark);
    FreeBookmark(TempBookmark);
    EnableControls;
  end;
end;

Lookup value when user typing it

Posted in Databases

This example displays value into NAME field at COUNTRY.DB database which have DBDEMOS alias.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Edit1.Text='' then Table1.First
  else Table1.Locate('NAME',Edit1.Text,[loPartialKey]);
end;

Insert/Update/Delete using by SQL

Posted in Databases

This example uses COUNTRY.DB database of DBDEMOS standard alias. SQL is a power tool for working with database. This example shows 3 main operations with database (Insert, Update and Delete record).

// Insert record
procedure TForm1.Button1Click(Sender: TObject);
begin
  with Query1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Insert into country(NAME,CAPITAL,CONTINENT,AREA,POPULATION) 
               values(
                 ''A_My_Country'', 
                 ''A_My_Capital'', 
                 ''A_My_Continent'', 
                 1, 
                 1)');
    ExecSQL;
  end;
  Table1.Refresh;
end;

// Delete record
procedure TForm1.Button2Click(Sender: TObject);
begin
  with Query1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Delete from country where name=''A_My_Country''');
    ExecSQL;
  end;
  Table1.Refresh;
end;

//Update record
procedure TForm1.Button3Click(Sender: TObject);
begin
  with Query1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Update country set name=''A_Your_Country'' 
               where name=''A_My_Country''');
    ExecSQL;
  end;
  Table1.Refresh;
end;

Insert/Edit/Delete methods

Posted in Databases

This example uses COUNTRY.DB database of DBDEMOS standard alias. To insert/update/delete record to/of/from database, you may use standard methods of Table component.

// Insert record
procedure TForm1.Button1Click(Sender: TObject);
begin
  Table1.InsertRecord(['A_My_Country', 
                       'A_My_Capital', 
                       'A_My_Continent', 
                       1,
                       1]);
end;

// Delete record
procedure TForm1.Button2Click(Sender: TObject);
begin
  Table1.Locate('NAME', 'A_My_Country', [loPartialKey]);
  Table1.Delete;
end;

// Update record
procedure TForm1.Button3Click(Sender: TObject);
begin
  with Table1 do
  begin
    Locate('NAME', 'A_My_Country', [loPartialKey]);
    Edit;
    FieldByName('NAME').AsString:='A_Your_Country';
    Post;
    Refresh;
  end;
end;

Get values from DBGrid

Posted in Databases

If you want to get values of all columns of selected row from DBGrid component, then use Fields and FieldCount properties, like in this example.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Memo1.Clear;
  with DBGrid1 do
  begin
    for i:=0 to FieldCount-1 do
      Memo1.Lines.Add(Fields[i].AsString);
  end;
end;