How to customize DBAdvGrid Columns in order to display data from database?

2.2k Views Asked by At

I am trying to display data from database table into DBAdvGrid so that first column will show only checkbox, while other 3 columns will show label, description and image field from database table. I am using UniConnection, UniQuery, UniDatasource components to display data. I am setting UniConnection Database properties via code as I can give relative path of the database rather than full path. At object inspector it accepts only fiull path of database.

My code is as follows At FormCreate event

  filepath1 := ExtractFilePath(Application.ExeName);
  UniConnection1.Database := filepath1+'empdata.s3db';
  UniConnection1.Connected := True;
  UniQuery1.SQL.Text := 'Select '', label, description, image from emp';
  UniQuery1.Open;

  DBAdvGrid1.Columns[0].FieldName := '';
  DBAdvGrid1.Columns[2].FieldName := 'label';
  DBAdvGrid1.Columns[3].FieldName := 'description';
  DBAdvGrid1.Columns[4].FieldName := 'image';

The above code connect and display data successfully but problem is with displaying data at DBAdvGrid as I want first column to display only checkbox (no data from DB). At DBAdvGrid I cannot set field name of particular column through object inspector as it is connecting to the database through code with by extracting path.

I have set first column of DBAdvGrid editor type property to the edDataCheckBox so that it would display checkbox in first column but it is getting overlapped by label data, similarly at second column it is showing 'description' and at 3rd column image.

How to customize it via code so that it would show

1st column - No Data
2nd column - label
3rd column - description
4th column - image

What I am getting as

1st column - label overlapped with checkbox
2nd column - description
3rd column - image {MEMO}
4th column - 

How to sort out this problem ?

enter image description here

1

There are 1 best solutions below

2
On

Here is the way that I draw a checkbox in a grid: the DrawColumnCell event has to be defined as follows

const
 IsChecked : array[0..1] of Integer =
             (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);

procedure TForm.DBAdvGrid1DrawColumnCell(Sender: TObject;
          const Rect: TRect; DataCol: Integer; Column: TColumn;
          State: TGridDrawState);
var
 DrawRect: TRect;

begin
 if column.Index = 1 then  
  begin
   DrawRect:= Rect;
   drawrect.left:= rect.left + 24;
   InflateRect (DrawRect, -1, -1);
   DBAdvGrid1.Canvas.FillRect (Rect);
   DrawFrameControl (DBAdvGrid1.Canvas.Handle, DrawRect, DFC_BUTTON,
                     ISChecked[Column.Field.AsInteger]);
  end
 else DBAdvGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;