After DBGrid to dynamic assignment, how to specify a row with a program as the current focus? How is mainly designated column, row, I was able to specify, with DBGrid1.DataSource.DataSet.RecNo: = 2; on the line, but I will not be listed, please teach me. Thank you!
让第3行第2列(“用户名称”列)获得焦点: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB; type TForm1 = class(TForm) ADOTable1: TADOTable; DataSource1: TDataSource; DBGrid1: TDBGrid; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ADOTable1.RecNo := 3; ADOTable1.FieldByName('用户名称').FocusControl; end; end.
If you want to get the focus are two (starting field array subscript 0):
ADOTable1.RecNo: =. 3;
ADOTable1.Fields [. 1] .FocusControl;
There is a problem, I modify the window is Form_ShangPinZiLiaoModi.ShowModal; pop-up, so
Form_ShangPinZiLiaoModi.Close; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.RecNo: = CurrentNumber; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.Fields [3] .FocusControl;
appear error, error contents is "can not focus a disabled or invisible window",
how should I change it?
Thank you!
"Can not focus a disabled or invisible window" is the cause of the error window DBGrid1 where not displayed, the operation of a Focus control it in the window must have been showing up. So you need to
Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.RecNo: = CurrentNumber; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.Fields [3 ] .FocusControl;
in the window displayed later operations.
In addition FocusControl method is a method of field objects, controls all data sets should be possible.
窗口2打开时焦点定位在DBGrid1的3行2列: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses unit2; procedure TForm1.Button1Click(Sender: TObject); begin Form2.ShowModal; end; end. ************************************************ unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, Grids, DBGrids; type TForm2 = class(TForm) DataSource1: TDataSource; DBGrid1: TDBGrid; ADOTable1: TADOTable; procedure FormShow(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.FormShow(Sender: TObject); begin ADOTable1.RecNo := 3; ADOTable1.Fields[1].FocusControl; end; end.
Or change the bad. MDI is my procedure, in the MainForm Form1.ShowModal, Form1 in Form2.ShowModal, Form1 have the DBGrid, Form2 window is modified, after modifying Form2 Form1.DBGrid1.Fields [ . 3 ] .FocusControl; Error. Form2 do not directly useful in Form1. Really bad change.
Form1.DBGrid1.Fields [after what you have done operations on Form2 3 ] .FocusControl out wrong? The code Tieshanglai better identify the problem.
In MainForm: Procedure TMainForm.Button1Click (Sender: TObject); the begin Form1.ShowModal; End ; the Form1 in: Procedure TForm1.FormShow (Sender: TObject); the begin the try IF DataModule1.ADOConnection1.Connected the then the begin SQL2: = ' SELECT XuHao AS number, zhengshuhao as certificate number, kuanhao as section number, ' + ' Xiaojia AS selling price, rukuliang as storage amount, zhiquanhao as finger ring number, ' + ' shouhuoriqi AS receipt date, xiaoshouriqi as sales date, fanghuodanwei ' + ' delivery of goods as a unit, from KuCunInfo Order ID as NID by xuhao '; DataModule1.ADOQuery1.Close; DataModule1.ADOQuery1.SQL.Text: = SQL2; DataModule1.ADOQuery1.ExecSQL; DataModule1.ADOQuery1.Active: = to true; End ; the finally End ; End ; Procedure TForm1.Button1Click (Sender: TObject); the begin { modify data in KuCunInfo } the try Form1.DBGrid1.Fields [ . 3 ] .FocusControl; the finally End ; End ; thus wrong without modification in Form2.
Haha, I get out. Search on Monopoly. Form1.DBGrid1.SelectedIndex: = . 4 ; Form1.DBGrid1.SetFocus; This trip. thank you!
Procedure TForm1.Button1Click (Sender: TObject); the begin { modify data in KuCunInfo } // After modifying inventory information, since the data Form1 or old, it is necessary to update the look here in Form1 // data, namely: reopen DBGrid1 a corresponding set of control data (DataModule1.ADOQuery1), and // then perform FocusControl the try Form1.DBGrid1.Fields [ . 3 ] .FocusControl; the finally End ; End ; Further the following code: DataModule1.ADOQuery1.Close; DataModule1. ADOQuery1.SQL.Text: = SQL2; DataModule1.ADOQuery1.ExecSQL; DataModule1.ADOQuery1.Active: = to true; recommended wrote: DataModule1.ADOQuery1.Close; DataModule1.ADOQuery1.SQl.Clear; DataModule1.ADOQuery1.SQL.Add (SQL2); DataModule1.ADOQuery1.Open; ADOQuery1 control, if it is using the Open SQL select statement, if it is Insert, Update, the Delete and so do not need to return value SQL with ExecSQL.