I wrote this quiet a while back and works up to Delphi 2007 for me without a problem
Uses Db,dbGrids,DbTables;
procedure PopulateDBGrid(DataSource: TDataSource; var RDBGrid: TDBGrid; DataFields, DataDescript, DataRO: array of const);
var nCurrec: Integer;
lReadOnly: Boolean;
TempVar: string;
begin
If not RDBGrid.Visible Then
RDBGrid.Visible := True;
RDBGrid.DataSource := DataSource;
RDBGrid.Columns.Clear;
for nCurRec := 0 to High(DataFields) do begin
RDBGrid.Columns.Add;
RDBGrid.Columns[nCurRec].FieldName := DataFields[nCurRec].VPChar;
if High(DataRO) >= nCurRec then
RDBGrid.Columns[nCurRec].ReadOnly := DataRO[nCurRec].VBoolean;
if High(DataDescript) >= nCurRec then
RDBGrid.Columns[nCurRec].Title.Caption := DataDescript[nCurRec].VPChar;
end;
RDBGrid.Repaint;
end;
Usage :
PopulateDBGrid(dtaAuto, DBGrid1, ['AccounNo', 'Name', 'TelNo'], ['Account No', 'Name', 'Telephone No'], [True, True, False]);
dtaAuto is the datasource to use to populate the DBGrid
DBGrid1 is the DBGrid to be populated
['AccounNo', 'Name', 'TelNo'] is the table fields to display
['Account No', 'Name', 'Telephone No'] is the Column Headings that will be displayed
[True, True, False] specifies if the field is readonly or editable (True = Readonly and Fasle Editable)
I have been trying to get this to work since XE6 but for some or other reason the DBgrid just will not display anything, If someone can figure it out I would appreciated it.
Uses Db,dbGrids,DbTables;
procedure PopulateDBGrid(DataSource: TDataSource; var RDBGrid: TDBGrid; DataFields, DataDescript, DataRO: array of const);
var nCurrec: Integer;
lReadOnly: Boolean;
TempVar: string;
begin
If not RDBGrid.Visible Then
RDBGrid.Visible := True;
RDBGrid.DataSource := DataSource;
RDBGrid.Columns.Clear;
for nCurRec := 0 to High(DataFields) do begin
RDBGrid.Columns.Add;
RDBGrid.Columns[nCurRec].FieldName := DataFields[nCurRec].VPChar;
if High(DataRO) >= nCurRec then
RDBGrid.Columns[nCurRec].ReadOnly := DataRO[nCurRec].VBoolean;
if High(DataDescript) >= nCurRec then
RDBGrid.Columns[nCurRec].Title.Caption := DataDescript[nCurRec].VPChar;
end;
RDBGrid.Repaint;
end;
Usage :
PopulateDBGrid(dtaAuto, DBGrid1, ['AccounNo', 'Name', 'TelNo'], ['Account No', 'Name', 'Telephone No'], [True, True, False]);
dtaAuto is the datasource to use to populate the DBGrid
DBGrid1 is the DBGrid to be populated
['AccounNo', 'Name', 'TelNo'] is the table fields to display
['Account No', 'Name', 'Telephone No'] is the Column Headings that will be displayed
[True, True, False] specifies if the field is readonly or editable (True = Readonly and Fasle Editable)
I have been trying to get this to work since XE6 but for some or other reason the DBgrid just will not display anything, If someone can figure it out I would appreciated it.