可以. if dbgrid1.SelectedField.FieldName='abc' then begin end else if dbgrid1.SelectedField.FieldName='def' then begin end;
if dbgrid1.SelectedField.FieldName='abc' then begin with TForm1.Create(nil) do try // Property1, Property2為TForm1的屬性 dbgrid1.SelectedField.AsString:= Property1; dbgrid1.SelectedField.AsString:= Property2; finally Free; end; end else if dbgrid1.SelectedField.FieldName='def' then begin end;type TForm1=class(TForm) public property property1 read Getproperty1 ; property property2 read Getproperty2 ; end;
还是看不懂,请高手讲详细点。最后有个DEMO。 谢谢了。
if dbgrid1.SelectedField.FieldName='abc' 在哪个事件中写这些代码? 我在ColEnter中没作用。急。
在form1的单元 中,写公共函数 如:function ReturnValue :string; function ReturnValue :string; begin result := ''; form1 := Tform1.Create(nil); with TForm1 do try if showModal=mrok then result := form1.某值 finally FreeAndNil(form1); end; end;在dbgrid的onKeypress事件里 procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char); begin with TDBGrid(Sender) do begin if SelectedField.FieldName = 'abc' then SelectedField.AsString := ReturnValue; end; end;
object Form1: TForm1 Left = 198 Top = 156 Width = 544 Height = 375 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object DBGrid1: TDBGrid Left = 52 Top = 72 Width = 443 Height = 193 DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] OnKeyDown = DBGrid1KeyDown Columns = < item Expanded = False FieldName = 'AA' Visible = True end item Expanded = False FieldName = 'BB' Visible = True end item Color = clInfoBk Expanded = False FieldName = 'CC' ReadOnly = True Visible = True end item Expanded = False FieldName = 'DD' Visible = True end item Expanded = False FieldName = 'EE' Visible = True end item Color = clInfoBk Expanded = False FieldName = 'FF' ReadOnly = True Visible = True end> end object Button1: TButton Left = 147 Top = 32 Width = 75 Height = 25 Caption = 'switch' TabOrder = 1 OnClick = Button1Click end object ADOQuery1: TADOQuery Connection = Form2.ADOConnection1 CursorType = ctStatic Parameters = <> SQL.Strings = ( 'select * from testtemp') Left = 62 Top = 25 end object DataSource1: TDataSource DataSet = ADOQuery1 Left = 31 Top = 56 end end把这个保存成Unit1.DFM
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, Grids, DBGrids, ADODB,Unit2, StdCtrls;type TForm1 = class(TForm) ADOQuery1: TADOQuery; DBGrid1: TDBGrid; DataSource1: TDataSource; Button1: TButton; procedure DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); var aa,cc,ff:string; begin if (Key=13) then begin if (DBGrid1.SelectedField.FieldName='AA') then begin DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1; aa:=ADOQuery1.FieldByName('AA').AsString; if SelectCCFF(aa,cc,ff) then begin if not (ADOQuery1.State in [dsEdit,dsInsert])then ADOQuery1.Edit; ADOQuery1.FieldByName('CC').AsString:=cc; ADOQuery1.FieldByName('FF').AsString:=ff; end else begin ShowMessage('没有找到'); DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex-1; end; end else begin DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1; if DBGrid1.SelectedIndex=DBGrid1.Columns.Count-1 then begin ADOQuery1.Append; DBGrid1.SelectedIndex:=0; end; end; end; end;procedure TForm1.Button1Click(Sender: TObject); begin ADOQuery1.Active:=not ADOQuery1.Active; end;end.把这个保存成Unit1.Pas
object Form2: TForm2 Left = 161 Top = 101 Width = 544 Height = 375 Caption = 'Form2' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object DBGrid2: TDBGrid Left = 6 Top = 39 Width = 527 Height = 265 DataSource = DataSource1 Options = [dgEditing, dgAlwaysShowEditor, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit] ReadOnly = True TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] OnDblClick = DBGrid2DblClick OnKeyPress = DBGrid2KeyPress end object ADOConnection1: TADOConnection ConnectionString = 'Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ' + 'ID=sa;Initial Catalog=abc;Data Source=.' LoginPrompt = False Provider = 'SQLOLEDB.1' Left = 14 Top = 8 end object ADOQuery1: TADOQuery Connection = ADOConnection1 Parameters = <> Left = 43 Top = 10 end object DataSource1: TDataSource DataSet = ADOQuery1 Left = 72 Top = 10 end end这个保存成Unit2.DFM
unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;type TForm2 = class(TForm) DBGrid2: TDBGrid; ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; DataSource1: TDataSource; procedure DBGrid2DblClick(Sender: TObject); procedure DBGrid2KeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end;var Form2: TForm2;function SelectCCFF(AA:string;var CC,FF:string):Boolean;implementation{$R *.dfm}function SelectCCFF(AA:string;var CC,FF:string):Boolean; function SetValue:Boolean; begin CC:=Form2.ADOQuery1.FieldByName('CC').AsString; FF:=Form2.ADOQuery1.FieldByName('FF').AsString; Result:=True; end; begin with Form2 do begin ADOQuery1.Close; ADOQuery1.SQL.Text:=Format('Select * from testtemp2 where AA=''%s''',[AA]); ADOQuery1.Open; if ADOQuery1.RecordCount=1 then Result:=SetValue else begin if ShowModal=mrOk then Result:=SetValue else Result:=False; end; ADOQuery1.Close; end; end;procedure TForm2.DBGrid2DblClick(Sender: TObject); begin if ADOQuery1.RecordCount>0 then ModalResult:=mrOk else ModalResult:=mrCancel; end;procedure TForm2.DBGrid2KeyPress(Sender: TObject; var Key: Char); begin if Key=#13 then DBGrid2DblClick(Sender); end;end. 这个保存成Unit2.Pas
如果是用[ADO]Query连的DBGrid注意一下SQL语句里不要有Order By和Distict等
出发单击DBGrid事件调出Form把选择的项目拿到Form里搜索一下,注意先记录下所单击那个单元格的位置,,,,,替换咯
.........是不是=于没说啊?^_^
然后在DBGrid中按键的时候给这个参数赋值
form中根据参数的值来决定显示哪个表的数据供用户选择
if dbgrid1.SelectedField.FieldName='abc' then
begin
end
else if dbgrid1.SelectedField.FieldName='def' then
begin
end;
begin
with TForm1.Create(nil) do
try
// Property1, Property2為TForm1的屬性
dbgrid1.SelectedField.AsString:= Property1;
dbgrid1.SelectedField.AsString:= Property2;
finally
Free;
end;
end
else if dbgrid1.SelectedField.FieldName='def' then
begin
end;type
TForm1=class(TForm)
public
property property1 read Getproperty1 ;
property property2 read Getproperty2 ;
end;
在form1的单元 中,写公共函数 如:function ReturnValue :string;
function ReturnValue :string;
begin
result := '';
form1 := Tform1.Create(nil);
with TForm1 do
try
if showModal=mrok then
result := form1.某值
finally
FreeAndNil(form1);
end;
end;在dbgrid的onKeypress事件里
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
with TDBGrid(Sender) do
begin
if SelectedField.FieldName = 'abc' then
SelectedField.AsString := ReturnValue;
end;
end;
我在FORM1中DBGRID1, 有4个字段,AA,BB,DD,EE。AA是输入拼音代码,然后在这个字段的回车后,显示FORM2,根据AA内容过滤DBGRID2内容,在FORM2中有DBGRID2,有3个字段,AA,CC,FF。这个AA是拼音代码,CC是名称。 在选中的记录后回车,关闭FORM2。把 CC填入FORM1中的DBGRID1,然后让用户输入其他的,DD,EE内容然后POST。
谢谢各位了!
Left = 198
Top = 156
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 52
Top = 72
Width = 443
Height = 193
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnKeyDown = DBGrid1KeyDown
Columns = <
item
Expanded = False
FieldName = 'AA'
Visible = True
end
item
Expanded = False
FieldName = 'BB'
Visible = True
end
item
Color = clInfoBk
Expanded = False
FieldName = 'CC'
ReadOnly = True
Visible = True
end
item
Expanded = False
FieldName = 'DD'
Visible = True
end
item
Expanded = False
FieldName = 'EE'
Visible = True
end
item
Color = clInfoBk
Expanded = False
FieldName = 'FF'
ReadOnly = True
Visible = True
end>
end
object Button1: TButton
Left = 147
Top = 32
Width = 75
Height = 25
Caption = 'switch'
TabOrder = 1
OnClick = Button1Click
end
object ADOQuery1: TADOQuery
Connection = Form2.ADOConnection1
CursorType = ctStatic
Parameters = <>
SQL.Strings = (
'select * from testtemp')
Left = 62
Top = 25
end
object DataSource1: TDataSource
DataSet = ADOQuery1
Left = 31
Top = 56
end
end把这个保存成Unit1.DFM
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, ADODB,Unit2, StdCtrls;type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
aa,cc,ff:string;
begin
if (Key=13) then
begin
if (DBGrid1.SelectedField.FieldName='AA') then
begin
DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1;
aa:=ADOQuery1.FieldByName('AA').AsString;
if SelectCCFF(aa,cc,ff) then
begin
if not (ADOQuery1.State in [dsEdit,dsInsert])then
ADOQuery1.Edit;
ADOQuery1.FieldByName('CC').AsString:=cc;
ADOQuery1.FieldByName('FF').AsString:=ff;
end
else begin
ShowMessage('没有找到');
DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex-1;
end;
end
else begin
DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1;
if DBGrid1.SelectedIndex=DBGrid1.Columns.Count-1 then
begin
ADOQuery1.Append;
DBGrid1.SelectedIndex:=0;
end;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Active:=not ADOQuery1.Active;
end;end.把这个保存成Unit1.Pas
Left = 161
Top = 101
Width = 544
Height = 375
Caption = 'Form2'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid2: TDBGrid
Left = 6
Top = 39
Width = 527
Height = 265
DataSource = DataSource1
Options = [dgEditing, dgAlwaysShowEditor, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit]
ReadOnly = True
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnDblClick = DBGrid2DblClick
OnKeyPress = DBGrid2KeyPress
end
object ADOConnection1: TADOConnection
ConnectionString =
'Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ' +
'ID=sa;Initial Catalog=abc;Data Source=.'
LoginPrompt = False
Provider = 'SQLOLEDB.1'
Left = 14
Top = 8
end
object ADOQuery1: TADOQuery
Connection = ADOConnection1
Parameters = <>
Left = 43
Top = 10
end
object DataSource1: TDataSource
DataSet = ADOQuery1
Left = 72
Top = 10
end
end这个保存成Unit2.DFM
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;type
TForm2 = class(TForm)
DBGrid2: TDBGrid;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
procedure DBGrid2DblClick(Sender: TObject);
procedure DBGrid2KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;function SelectCCFF(AA:string;var CC,FF:string):Boolean;implementation{$R *.dfm}function SelectCCFF(AA:string;var CC,FF:string):Boolean;
function SetValue:Boolean;
begin
CC:=Form2.ADOQuery1.FieldByName('CC').AsString;
FF:=Form2.ADOQuery1.FieldByName('FF').AsString;
Result:=True;
end;
begin
with Form2 do
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text:=Format('Select * from testtemp2 where AA=''%s''',[AA]);
ADOQuery1.Open;
if ADOQuery1.RecordCount=1 then
Result:=SetValue
else begin
if ShowModal=mrOk then
Result:=SetValue
else Result:=False;
end;
ADOQuery1.Close;
end;
end;procedure TForm2.DBGrid2DblClick(Sender: TObject);
begin
if ADOQuery1.RecordCount>0 then
ModalResult:=mrOk
else ModalResult:=mrCancel;
end;procedure TForm2.DBGrid2KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
DBGrid2DblClick(Sender);
end;end.
这个保存成Unit2.Pas
运行看看。
http://www.delphipages.cn/dispbbs.asp?boardid=28&id=539