2007. szeptember 8., szombat

Reference a column of a TDBGrid by name instead of integer index


Problem/Question/Abstract:

Is there a way in TDBGrid to reference a column by name rather than by integer index? Right now I am using "ListGrd.Columns[ 5 ]" (for example) to access a particular column but that is dangerous if moving columns is enabled. Can I reference a column by a column name instead?

Answer:

function TForm1.ColumnByFieldName(AGrid: TDBGrid; const AFieldName: string): TColumn;
var
  I: Integer;
begin
  for I := 0 to AGrid.Columns.Count - 1 do
  begin
    Result := AGrid.Columns[I];
    if AnsiCompareText(Result.FieldName, AFieldName) = 0 then
      Exit;
  end;
  raise Exception.Create(AGrid.Name + ', ' + AFieldName);
end;

Nincsenek megjegyzések:

Megjegyzés küldése