把某字段内容都截取以211开头的前5位数字,然后在dbgrid中显示截取字段内容,此段程序应该如何写?

解决方案 »

  1.   

    在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;
      

  2.   

    刚才忘了,补充一点,在Dbgrid中要显示字段必须是编号1,否则是看不到的。
      

  3.   

    你加个判断不就行了。
    或者用SQL条件查询。
      

  4.   

    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;
      

  5.   

    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
      

  6.   

    frogshero(三波萝)的方法应该没有问题的