在AdoDateSet中创建新的字段,然后用AdoDateSet的onCalcFileds事件设置字段值。比如你的加的字段是“编号1”,那么在事件中要这样写: procedure TMainForm.ADODataSet1CalcFields(DataSet: TDataSet); begin If not ADODataSet1.FieldByName('编号').IsNull Then ADODataSet1.FieldByName('编号1').AsString := copy(ADODataSet1.FieldByName('编号').AsString,1,5); end;
刚才忘了,补充一点,在Dbgrid中要显示字段必须是编号1,否则是看不到的。
你加个判断不就行了。 或者用SQL条件查询。
select left(field1,5) from txxx where field1 like '211%' 或者在永久字段的ONGetText事件改變顯示: procedure TForm1.Table1ADDRESS_1GetText(Sender: TField; var Text: String; DisplayText: Boolean); begin text:=Copy(Sender.AsString,0,5); end;
adoquery1.close; adoquery1.sql.clear; adoquery1.sql.text:='select substring(field1,1,5) from txxx where field1 like '211+'%'''; adoquery1.open; 然后datasource1链adoquery1 dbgrid1链datasource1
procedure TMainForm.ADODataSet1CalcFields(DataSet: TDataSet);
begin
If not ADODataSet1.FieldByName('编号').IsNull Then
ADODataSet1.FieldByName('编号1').AsString := copy(ADODataSet1.FieldByName('编号').AsString,1,5);
end;
或者用SQL条件查询。
或者在永久字段的ONGetText事件改變顯示:
procedure TForm1.Table1ADDRESS_1GetText(Sender: TField; var Text: String;
DisplayText: Boolean);
begin
text:=Copy(Sender.AsString,0,5);
end;
adoquery1.sql.clear;
adoquery1.sql.text:='select substring(field1,1,5) from txxx where field1 like '211+'%''';
adoquery1.open;
然后datasource1链adoquery1
dbgrid1链datasource1