  ==================================================
                  RSD Tips&Tricks
               Based on the questions 
            to the RSD Consulting Service
                 (free for a while)
  ==================================================
  Authors:                             Supported by:
  Andrey Telnov And Roman Eremin        RSD software
  rsd@tibc.tula.ru              http://www.rsd.pp.ru
  ..................................................
  April 10, 1997               RSD Tips&Tricks   #01
  ..................................................

Contents 

1. Multiselect option in the DBGrid (demo)
2. Reindex all of the indexes in the alias (demo)
3. Write to ini files (demo)
4. Change the color and Font of the DBGrid rows (demo + component)

1. Multiselect option in the DBGrid
Q:
I often seen the multiselect option in a DBGrid - if you press ctrl and 
click you can select more than one record in a grid. However...
nowhere in the documentation does it tell you how to work with the 
records that you selected.

A:
You are right. Borland guys created the posibility to use the multiselect in the DBGrid but didn't document it.

You have to use the public property  SelectedRows;
property SelectedRows: TBookmarkList;

You may:
a) Unselect all rows:
   DBGrid1.SelectedRows.Clear;
b) Delete all selected rows:
   DBGrid1.SelectedRows.Delete;
   (I advice to ask the user about deleting the records before you do it)
c) Get the Count of the selected rows:
   MessageBox('You have selected ' + IntToStr(DBGrid1.SelectedRows.Count) + ' row(s)');

For more information see the example #001 (Example of using multiselect options in a DBGrid)

2. Reindex all of the indexes in the alias
Q:
How I can read the structure of a DB file and then reindex all of 
the indexes and  I can not make a type of progress indicator?
A:

a) To reindex the table you have to use the BDE function

DbiRegenIndexes(Handle: HDBICur);
The cursor of the DB Table has to be open. Use also 
Check function to fetch the DB errors.

b) To get all tables of the alias use GetTableNames method of the TSession component:
procedure GetTableNames(const DatabaseName, Pattern: string;
  Extensions, SystemTables: Boolean; List: TStrings);

c) and to display the progress indicator use: ProcessMessages method of the Application variant.

So you have to write the follow code:

procedure Reindex(DataBase : TDataBase; Table : TTable);
Var
  List: TStrings;
  i : Integer;
begin
  {Try to connect}
  try
    Database.Connected := True;
  except
    raise;
    exit;
  end;
  {Create the Table names list}
  List := TStringList.Create;
  {Get the Table names list}
  Session.GetTableNames(Database.AliasName, '', True, False, List);
  {Reindex all tables}
  for i := 0 to List.Count - 1 do begin
    {Display some information about the progress}
    {Proceed windows to }
    Application.ProcessMessages;
    Table.Close;
    {Assing the table name}
    Table.TableName := List[i];
    {Try to reindex}
    try
      {Open table}
      Table.Open;
      {reindex}
      Check(DbiRegenIndexes(Table.Handle));
    except
      {Display error}
      raise;
    end;
  end;
  {Destroy the Table names list}
  List.Free;
  {Disconnect from DataBase}
  Database.Connected := False;
end;


For more information see example # 002 (Reindex all tables of the selected alias)

3. Write to ini files
Q:
I wanted to write a simple Delphi application that must write an 
ini file for me based on buttons pressed, i.e
Button 1 pressed, Button 2 not pressed, Button 3 pressed
How will I go about it to write such an application?
A:
It is very simple. Use TIniFile object (unit IniFiles).
1) Create ini object: 
  myinifile := TIniFile.Create('myinifile.ini');
2) Read from inifile
a) to read string from ini file  ReadString method
b) to read boolean value from ini file  ReadBool method
c) to read integer value from ini file  ReadInteger method
3) Write in ini file
a) to write string from ini file WriteString method
b) to write boolean value from ini file WriteBool method
c) to write integer value from ini file WriteInteger method
4) And don't fogert to destroy your TIniFlile object
  myinifile.Free;

For more information see  example # 003 (Example of using ini file).

4. Change the color and Font of the  DBGrid rows
Q: 
How is possible to alternate two colors for the rows of a DbGrid ?
And what's the easy way to change the color of a cell ... for example
if there is a negative number ? (G. C.)
A:
At first I thought it is very simple question. We all know about the color property of the TDBColumn. So we have to change this property on OnDrawColumnCell and that is all.
But then i notice that Delphi redraws the column after changing the column color or font. 

That's why this code doesn't work !
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if(Column.Field = Table1.FindField('payment')) And
    (Table1.FindField('payment').AsFloat > 1000) then
     Column.Color := clBlue;
end;

There is only one way to resolve this problem:
Draw the rect by yourself. 
But I done the easy way change the color of a cell. Use the TDrawDBGrid component. I make it for fun. I add the event onDrawFieldCellEvent:
procedure TForm2.DrawDBGrid1DrawFieldCellEvent(Sender: TObject;
  Field: TField; var Color: TColor; var Font: TFont);
begin
  if(Field = Table1.FindField('payment')) And
    (Table1.FindField('payment').AsFloat > 1000) then
      Color := clBlue;
end; 

This code workes properly !

Install: drdbgrid.pas and see example drdbgrpr.dpr:
Example # 004  (TDrawDBGrid component)

Any comments and sugestions
please e-mail: rsd@tibc.tula.ru 

For questions about Delphi
please e-mail: rsd@tibc.tula.ru 