| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1097 人关注过本帖
标题:Delphi数据显示问题
取消只看楼主 加入收藏
yltf
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-24
收藏
 问题点数:0 回复次数:0 
Delphi数据显示问题
大家好, 我现在遇到一个问题, 我用TDBGrid与数据源绑定了, 然后有个查询的按钮, 点击第一次可以正常显示数据, 但多点击几次查询, 下面的表格就默认显示第二条数据, 而不是显示第一数据.
google了挺长时间, 也没有找到问题的解决办法, 现在想请大家帮个忙, 帮我看下这个问题.

delphi代码:
unit truck;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBTables, ExtCtrls, ComCtrls, IniFiles, Grids,
  DBGrids, xmldom, XMLIntf, msxmldom, XMLDoc, ADODB, DBCtrls,DateUtils;

type
  TF_truck = class(TForm)
    Database11: TDatabase;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    E_dsn: TEdit;
    E_exportfile: TEdit;
    B_choosedescfile: TButton;
    Button4: TButton;
    Panel2: TPanel;
    Label3: TLabel;
    D_date1: TDateTimePicker;
    Button2: TButton;
    Label5: TLabel;
    E_factoryID: TEdit;
    D_time1: TDateTimePicker;
    D_date2: TDateTimePicker;
    D_time2: TDateTimePicker;
    Label4: TLabel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Q_boxweight1: TQuery;
    Q_grossweight1: TQuery;
    DataSource_boxweight: TDataSource;
    DataSource_grossweight: TDataSource;
    DBGrid2: TDBGrid;
    S_exportfile: TSaveDialog;
    XMLDocument: TXMLDocument;
    Database1: TADOConnection;
    Q_boxweight: TADOQuery;
    Q_grossweight: TADOQuery;
    TabSheet3: TTabSheet;
    Memo1: TMemo;
    StatusBar1: TStatusBar;
    Label6: TLabel;
    Edit1: TEdit;
    Label7: TLabel;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure B_choosedescfileClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    function getBoxWeight(boxID: String):Real;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  
const
  INIFILENAME:string='config.ini';
var
  F_truck: TF_truck;
  descFilePath: String;
  descFileName: String;
  iniFilePathName: String;
  constSqlStr: String;
  factoryID: String;
  dsnName: String;
  myIniFile: TIniFile;
  boxesweigh:TStringList;
implementation

{$R *.dfm}

procedure TF_truck.Button1Click(Sender: TObject);
//var
  //i:Integer;
begin
  if(Trim(E_dsn.Text)='')then
  begin
    ShowMessage('请输入dsn名称!');
    E_dsn.SetFocus;
  end
  else
  begin
    Database1.Close;
    //showmessage(Database1.ConnectionString);
    //showmessage(copy(Database1.ConnectionString,1,pos('Data Source=',Database1.ConnectionString)+11));
    Database1.ConnectionString:='Provider=MSDASQL.1;Password=123;User ID=xtqch;Persist Security Info=True;Data Source='+trim(E_Dsn.Text);
    if not Database1.Connected then
    begin
      myIniFile.WriteString('DSN','dsnName',Trim(E_dsn.Text));
      {for i:=0 to Database1.Params.Count-1 do
        if(Pos('ODBC DSN',Database1.Params[i])>0)then
          Database1.Params[i]:='ODBC DSN='+Trim(E_dsn.Text);}
    end;
    try
      StatusBar1.SimpleText:='正在查询数据……';
      myIniFile.WriteString('factory','ID',Trim(E_factoryID.Text));
      //ShortDateFormat:='yyyy-mm.dd';
      if not Database1.Connected then
        Database1.Open;
      {Q_boxweight.Close;
      Q_boxweight.Open;
      Q_boxweight.First;
      boxesweigh.Clear;
      while not Q_boxweight.Eof do
      begin
        boxesweigh.Add(Q_boxweight.FieldByName('车号').AsString);
        boxesweigh.Add(Q_boxweight.FieldByName('空车重量').AsString);
        Q_boxweight.Next;
      end;}
      Q_grossweight.Close;
      Q_grossweight.SQL.Clear;
      Q_grossweight.SQL.Append('select * from cheng_main where ');
      Q_grossweight.SQL.Append('(guo_riqi>'''+FormatDateTime('yyyy-mm-dd',D_date1.Date)+' '+FormatDateTime('hh:mm:ss',D_time1.Time)+''' and guo_riqi<'''+FormatDateTime('yyyy-mm-dd',D_date2.Date)+' '+FormatDateTime('hh:mm:ss',D_time2.Time)+''') ');
      if(Trim(Edit1.Text)<>'')then
      begin
      Q_grossweight.SQL.Append('and prn_l='''+Edit1.Text+''' ');
      end;
      if(Trim(Edit2.Text)<>'')then
      begin
      Q_grossweight.SQL.Append('and dan_wei like ''%'+Edit2.Text+'%'' ');
      end;
      Q_grossweight.SQL.Append('and car_net <> 0 ');
      Q_grossweight.SQL.Append('order by guo_riqi desc');
      //showmessage(Q_grossweight.SQL.Text);
      Q_grossweight.Open;
       StatusBar1.SimpleText:='查询完成(共找到'+IntToStr(Q_grossweight.RecordCount)+'条记录)';
    except
      on E:Exception do
        showmessage(e.Message);
        //ShowMessage('数据库不能成功打开!');
    end;
  end;

end;

procedure TF_truck.FormCreate(Sender: TObject);
var
  DateTime1:String;
begin
  iniFilePathName:=ExtractFilePath(application.ExeName)+INIFILENAME;
  myIniFile := TIniFile.Create(iniFilePathName);
  descFilePath:= myIniFile.ReadString('descPath', 'filepath','');

  if(not(Trim(descFilePath)=''))then
    E_exportfile.Text:=descFilePath+StringReplace(DateTimeToStr(Now),':','.',[rfReplaceAll, rfIgnoreCase])+'.xml';
  factoryID:= myIniFile.ReadString('factory', 'ID','');
  if(not(Trim(factoryID)=''))then
    E_factoryID.Text:=factoryID;
  dsnName:= myIniFile.ReadString('DSN', 'dsnName','');
  if(not(Trim(dsnName)=''))then
    E_dsn.Text:=dsnName;
  boxesweigh:=TStringList.Create;
  DateTime1:=myIniFile.ReadString('time', 'exportTime','');
  if(DateTime1<>'')then
    begin
      D_date1.DateTime:=StrToDateTime(DateTime1);
      D_time1.DateTime:=StrToDateTime(DateTime1);
    end
  else
    begin
      D_date1.DateTime:=DateUtils.StartOfTheMonth(Now);
      D_time1.DateTime:=StrToDateTime('00:00:00');
    end;

  D_date2.DateTime:=Now;
  D_time2.DateTime:=Now;
end;

procedure TF_truck.B_choosedescfileClick(Sender: TObject);
begin
  S_exportfile.FileName:=StringReplace(DateTimeToStr(Now),':','.',[rfReplaceAll, rfIgnoreCase]);
  if(S_exportfile.Execute)then
  begin
    descFilePath:=ExtractFilePath(S_exportfile.FileName);
    descFileName:=ExtractFileName(S_exportfile.FileName);
    E_exportfile.Text:=S_exportfile.FileName;
    myIniFile.WriteString('descPath', 'filepath',descFilePath);
    myIniFile.WriteString('descPath', 'filename',descFileName);
  end;  
end;

procedure TF_truck.Button4Click(Sender: TObject);
var
  root,node,data,item:IXMLNode;
  tmp_box,tmp_decr:Real;
  records,fields:TStringList;
  i,j:integer;
  tmp:String;

begin
  if(Trim(E_exportfile.Text)='')then
  begin
    ShowMessage('请输入将被导出的文件!');
    B_choosedescfile.Click;
  end
  else
  begin
    if(not Q_grossweight.Active)then
      ShowMessage('没有数据被查询出来,请先查询出数据')
    else
    begin
      //形成XML导出文件--start
      StatusBar1.SimpleText:='遍历数据……';
      XMLDocument.Active:=true;
      XMLDocument.Encoding:='gb2312';
      root:=XMLDocument.Node;
      node:=root.AddChild('datas');
      node.Attributes['type']:='auto-weigh-data-import';
      node:=node.AddChild('factory');
      node.Attributes['code']:=factoryID;
      Q_grossweight.First;
      records:=TStringList.Create;
      while(not Q_grossweight.Eof)do
      begin
        try
          data:=node.AddChild('data');
          data.Attributes['serialno']:=Q_grossweight.FieldByName('guo_number').AsString;
          item:=data.AddChild('providerstoredid');
          item.Text:=Q_grossweight.FieldByName('dan_wei').AsString;
          item:=data.AddChild('boxlicence');
          item.Text:=Q_grossweight.FieldByName('car_id').AsString;
          item:=data.AddChild('weighttime');
          item.Text:=Q_grossweight.FieldByName('guo_riqi').AsString;
          item:=data.AddChild('grossweight');
          item.Text:=Q_grossweight.FieldByName('car_grass').AsString;
          item:=data.AddChild('deductweight');
          tmp_decr:=0;
          item.Text:=FloatToStr(tmp_decr);
          item:=data.AddChild('boxweight');
          tmp_box:=Q_grossweight.FieldByName('car_tare').AsFloat;
          item.Text:=FloatToStr(tmp_box);
          item:=data.AddChild('netweight');
          item.Text:=Q_grossweight.FieldByName('car_net').AsString;
          item:=data.AddChild('operatorname');
          item.Text:=Q_grossweight.FieldByName('si_cheng').AsString;
          item:=data.AddChild('operatorid');
          item:=data.AddChild('station');
          item.Text:='湘潭';
          item:=data.AddChild('stuff');
          item:=data.AddChild('boxkind');
          item.Text:='small';
          Q_grossweight.Next;
        except
          on e:exception do
            showmessage(fields[0]+':'+fields[4]+':'+fields[5]);
        end;
      end;
      {memo1.Clear;
      for i:=1 to records.Count do
      begin
        tmp:='';
        tmp:=inttostr(i);
        fields:=TStringList(records.Objects[i-1]);
        for j:=0 to fields.Count-1 do
          tmp:=tmp+','+fields[j];
        memo1.Lines.Add(tmp);
      end;}
      StatusBar1.SimpleText:='保存数据……';
      {for i:=0 to records.Count-1 do
      begin
        try
          fields:=TStringList(records.Objects[i]);
          data:=node.AddChild('data');
          data.Attributes['serialno']:=fields[0];//Q_grossweight.FieldByName('顺序号').AsString;
          item:=data.AddChild('providerstoredid');
          item.Text:=fields[1]+fields[2];//Q_grossweight.FieldByName('公司序号').AsString+Q_grossweight.FieldByName('矿名序号').AsString;
          item:=data.AddChild('boxlicence');
          item.Text:=fields[9];//Q_grossweight.FieldByName('车号').AsString;
          item:=data.AddChild('weighttime');
          item.Text:=StringReplace(fields[7],'.','-',[rfReplaceAll, rfIgnoreCase])+fields[8];//StringReplace(Q_grossweight.FieldByName('日期').AsString,'.','-',[rfReplaceAll, rfIgnoreCase])+' '+Q_grossweight.FieldByName('时间').AsString;
          item:=data.AddChild('grossweight');
          item.Text:=fields[3];//Q_grossweight.FieldByName('重量').AsString;
          item:=data.AddChild('deductweight');
          if trim(fields[4])='' then fields[4]:='0';
          if trim(fields[5])='' then fields[5]:='0';
          tmp_decr:=StrToFloat(fields[4])+StrToFloat(fields[5]);//Q_grossweight.FieldByName('超重').AsFloat+Q_grossweight.FieldByName('扣重').AsFloat;
          item.Text:=FloatToStr(tmp_decr);
          item:=data.AddChild('boxweight');
          tmp_box:=getBoxWeight(fields[9]);//Q_grossweight.FieldByName('车号').AsString);
          item.Text:=FloatToStr(tmp_box);
          item:=data.AddChild('netweight');
          item.Text:=fields[6];//Q_grossweight.FieldByName('实重').AsString;
          item:=data.AddChild('operatorname');
          item:=data.AddChild('operatorid');
          item:=data.AddChild('station');
          item:=data.AddChild('stuff');
          item:=data.AddChild('boxkind');
          item.Text:='small';
        except
          on e:exception do
            showmessage(fields[0]+':'+fields[4]+':'+fields[5]);
        end;
      end;}

      XMLDocument.SaveToFile(E_exportfile.Text);
      XMLDocument.Active:=false;
      StatusBar1.SimpleText:='导出文件保存成功!';
      ShowMessage('XML数据文件导出完毕!');
      myIniFile.WriteString('time', 'exportTime',DateToStr(D_Date2.Date)+' '+TimeToStr(D_Time2.Time));
      //形成XML导出文件--end
    end;
  end;
end;

function TF_truck.getBoxWeight(boxID: String): Real;
var
  i:Integer;
begin
  Result:=0;
  for i:=0 to boxesweigh.Count-1 do
  begin
    if(((i mod 2)=0)and(trim(boxesweigh[i])=boxID))then
    begin
      try
        Result:=StrToFloat(boxesweigh[i+1]);
        break;
      except
        on e:exception do
          showmessage(boxID+':'+boxesweigh[i+1]+'#13'+e.Message);
      end;
    end;
  end;
  //if(Q_boxweight.Locate('车号',boxID,[]))then
  //  Result:=Q_boxweight.FieldByName('空车重量').AsFloat;
end;

end.
搜索更多相关主题的帖子: Delphi google 数据 truck Forms 
2008-07-24 16:20
快速回复:Delphi数据显示问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026396 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved