场景:点击窗口1中的dbgrid后,弹出可以修改这个记录窗口2
问题:点击dbgrid的行后,在窗口2创建的同时,窗口1的dbgrid数据集跟着改变
环境:delphi7
代码://数据模块
unit Unit4;
interface
uses
  SysUtils, Classes, DB, ADODB,
  Dialogs,ComObj;
type
  TDataModule4 = class(TDataModule)
    ADOQuery1: TADOQuery;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    function getRs(temp_sql:String):TADOQuery;
    procedure setRs(temp_sql: string);
  published     
  end;
var
  DataModule4: TDataModule4;
implementation
uses Unit7;
{$R *.dfm}
procedure TDataModule4.DataModuleCreate(Sender: TObject);
begin
   ADOQuery1.ConnectionString := 'Provider=SQLOLEDB.1;'
    +'Integrated Security=SSPI;'
    +'Persist Security Info=False;'
    +'User ID=sa;'
    +'Password=x;'
    +'Initial Catalog=e;'
    +'Data Source=127.0.0.1';
end;      
function TDataModule4.getRs(temp_sql: String): TADOQuery;
begin
  //Showmessage('sql:'+temp_sql);
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add(temp_sql);
  try
    ADOQuery1.Open;
    Result := ADOQuery1;
  except
    on e:Exception do
      ShowMessage('Êý¾Ý¿â²Ù×÷Òì³££¬ÇëÁªÏµ¹ÜÀíÔ±£¡');
  end;
end;
procedure TDataModule4.setRs(temp_sql: string);
begin
  //Showmessage('temp_sql:'+temp_sql);
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add(temp_sql);  
  try
    ADOQuery1.ExecSQL;
  except
    on e:Exception do
    begin
      ShowMessage('Êý¾Ý¿â²Ù×÷Òì³££¬ÇëÁªÏµ¹ÜÀíÔ±£¡');
    end;
  end;
end;
end.
//窗口1
unit Unit11;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB,
  ADODB;
type
  TForm11 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    Button1: TButton;
    Edit1: TEdit;
    GroupBox2: TGroupBox;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form11: TForm11;
implementation
uses Unit7, Unit4, Unit5, Unit12;
{$R *.dfm}
procedure TForm11.FormCreate(Sender: TObject);
begin
  Form11.Caption := Unit7.f11_c;
  GroupBox1.Caption := Unit7.f11_g1;
  GroupBox2.Caption := Unit7.f11_g2;
  RadioButton1.Caption := Unit7.f11_r1;
  RadioButton2.Caption := Unit7.f11_r2;
  RadioButton3.Caption := Unit7.f11_r3;
  RadioButton4.Caption := Unit7.f11_r4;
  Button1.Caption := Unit7.f11_b1;
  Edit1.Text := '';
  DBGrid1.Columns[0].Title.Caption := Unit7.f11_dbg1_c0;
  DBGrid1.Columns[1].Title.Caption := Unit7.f11_dbg1_c1;
  DBGrid1.Columns[2].Title.Caption := Unit7.f11_dbg1_c2;
  DBGrid1.Columns[3].Title.Caption := Unit7.f11_dbg1_c3;
  DBGrid1.Columns[4].Title.Caption := Unit7.f11_dbg1_c4;
  DBGrid1.Columns[5].Title.Caption := Unit7.f11_dbg1_c5;
  DBGrid1.Columns[6].Title.Caption := Unit7.f11_dbg1_c6;
  DBGrid1.Columns[7].Title.Caption := Unit7.f11_dbg1_c7;
  DBGrid1.Columns[0].FieldName := 'id';
  DBGrid1.Columns[1].FieldName := 'c1';
  DBGrid1.Columns[2].FieldName := 'c2';
  DBGrid1.Columns[3].FieldName := 'c3';
  DBGrid1.Columns[4].FieldName := 'c4';
  DBGrid1.Columns[5].FieldName := 'c5';
  DBGrid1.Columns[6].FieldName := 'c6';
  DBGrid1.Columns[7].FieldName := 'c7';
  DBGrid1.Columns[0].Width := 50;
  DBGrid1.Columns[1].Width := 100;
  DBGrid1.Columns[2].Width := 150;
  DBGrid1.Columns[3].Width := 150;
  DBGrid1.Columns[4].Width := 50;
  DBGrid1.Columns[5].Width := 50;
  DBGrid1.Columns[6].Width := 100;
  DBGrid1.Columns[7].Width := 100;
end;
procedure TForm11.Button1Click(Sender: TObject);
  var
    temp_sql : string ;
    temp_adoquery : TADOQuery;
begin
  if RadioButton1.Checked then
  begin
    temp_sql := 'exec a808009b '''+Unit7.v_1+''','''+Unit7.v_2+''',''1'','''' ';
    temp_adoquery := Unit4.DataModule4.getRs(temp_sql);
    DataSource1.DataSet := temp_adoquery;
  end
  else if RadioButton2.Checked then
  begin
    temp_sql := 'exec a808009b '''+Unit7.v_1+''','''+Unit7.v_2+''',''2'','''' ';
    temp_adoquery := Unit4.DataModule4.getRs(temp_sql);
    DataSource1.DataSet := temp_adoquery;
  end
  else if RadioButton3.Checked then
  begin
    temp_sql := 'exec a808009b '''+Unit7.v_1+''','''+Unit7.v_2+''',''3'','''' ';
    temp_adoquery := Unit4.DataModule4.getRs(temp_sql);
    DataSource1.DataSet := temp_adoquery;
  end
  else if RadioButton4.Checked then
  begin
    temp_sql := 'exec a808009b '''+Unit7.v_1+''','''+Unit7.v_2+''',''4'','''+Edit1.Text+'''';
    temp_adoquery := Unit4.DataModule4.getRs(temp_sql);
    DataSource1.DataSet := temp_adoquery;
  end;
end;
procedure TForm11.DBGrid1DblClick(Sender: TObject);
begin
  if (DBGrid1.FieldCount>0) then
  begin
    Application.CreateForm(Unit12.TForm12,Form12);
    Form12.Edit1.Text := Unit7.v_1;
    Form12.Edit2.Text := Unit7.v_2;
    Form12.DateTimePicker1.DateTime := StrToDate(DBGrid1.Fields[1].Text);
    Form12.Edit3.Text := DBGrid1.Fields[2].Text;
    Form12.Edit4.Text := DBGrid1.Fields[3].Text;
    Form12.Edit5.Text := DBGrid1.Fields[4].Text;
    Form12.Edit7.Text := DBGrid1.Fields[7].Text;
    Form12.ShowModal;
    Form12.Free;
  end;
end;
end.
//窗口2
unit Unit12;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls,
  ADODB;
type
  TForm12 = class(TForm)
    Label1: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    DateTimePicker1: TDateTimePicker;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Edit3: TEdit;
    ListBox1: TListBox;
    Edit4: TEdit;
    ListBox2: TListBox;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    GroupBox3: TGroupBox;
    Label10: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    ListBox4: TListBox;
    ListBox5: TListBox;
    Edit9: TEdit;
    Edit10: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form12: TForm12;
implementation
uses Unit7, Unit5, Unit4;
{$R *.dfm}
procedure TForm12.FormCreate(Sender: TObject);
  var
    temp_sql12 : string;
    temp_adoquery12 : TADOQuery;
begin
  Form5.Caption := Unit7.f12_c;
  Label1.Caption := Unit7.f12_l1;
  Label2.Caption := Unit7.f12_l2;
  Label3.Caption := Unit7.f12_l3;
  Label4.Caption := Unit7.f12_l4;
  Label5.Caption := Unit7.f12_l5;
  Label6.Caption := Unit7.f12_l6;
  Label7.Caption := Unit7.f12_l7;
  Label8.Caption := Unit7.f12_l8;
  Label9.Caption := Unit7.f12_l9;
  Label10.Caption := Unit7.f12_l10;
  Label11.Caption := Unit7.f12_l11;
  Label13.Caption := Unit7.f12_l13;
  Label14.Caption := Unit7.f12_l14;
  GroupBox1.Caption := Unit7.f12_g1;
  GroupBox2.Caption := Unit7.f12_g2;
  GroupBox3.Caption := Unit7.f12_g3;
  Button1.Caption := Unit7.f12_b1;
  Button2.Caption := Unit7.f12_b2;
  Button3.Caption := Unit7.f12_b3;
  Edit6.Text := '';
  Edit7.Text := '';
  Edit9.Text := '';
  Edit10.Text := '';
  ListBox1.Clear;
  ListBox2.Clear;
  ListBox4.Clear;
  ListBox5.Clear;
  temp_sql12 := 'select distinct(c1) from a8002';
  temp_adoquery12 := Unit4.DataModule4.getRs(temp_sql12);
  if temp_adoquery12.RecordCount > 0 then
  begin
    while not temp_adoquery12.Eof do
    begin
      Form12.ListBox4.Items.Add(
        temp_adoquery12.FieldValues['c1']
      );
      temp_adoquery12.Next;
    end;
  end;    
end;
end.
//我是一名爱好者,请大家多多指教,谢谢

解决方案 »

  1.   

    我想你是想实现这样一个功能吧.
    当我点DBGRID1的时候,在窗口2对点中的记录进行明细显示,再做修改,
    这样来做,点DBGRID1.时给窗体2传送一个当前记录的ID,这个ID是唯一的了,哪么,你在窗口2用sql对这个ID进行明细表的一个筛选.数据源方面不会邦的话,好好看看DELPHI的ADO连接数据库知识.
      

  2.   

    功能就是你所得
    我已实现窗口1当双击窗口1后,窗口2中的数据集就获取到了,但同时就把窗口1的dbgrid数据给替换掉了
      

  3.   

    我看了 pengxuan 博客中关于 dbgrid 的显示问题后,感觉有点像
    于是我添加了
      temp_adoquery.Active := False;
      temp_adoquery.Active := True;窗口1的dbgrid显示数据受窗口2的影响已解决但有新问题,运行时提示:
    access violation at address 0031c31 .read of address 0031c31
    这个错误怎么解决