Reference a column of a TDBGrid by name instead of integer index (Views: 34)
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?
function TForm1.ColumnByFieldName(AGrid: TDBGrid; const AFieldName: string): TColumn;
for I := 0 to AGrid.Columns.Count - 1 do
Result := AGrid.Columns[I];
if AnsiCompareText(Result.FieldName, AFieldName) = 0 then
raise Exception.Create(AGrid.Name + ', ' + AFieldName);
<< Back to main page