当前位置:Gxlcms > 数据库问题 > 给DBGrid动态赋值后,如何用程序指定某行某列为当前焦点?(100分)

给DBGrid动态赋值后,如何用程序指定某行某列为当前焦点?(100分)

时间:2021-07-01 10:21:17 帮助过:9人阅读

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.

如果希望是第2列获得焦点(字段数组下标从0开始):
ADOTable1.RecNo := 3;
ADOTable1.Fields[1].FocusControl;

 

有一个问题,我的修改窗口是Form_ShangPinZiLiaoModi.ShowModal; 弹出的,所以
Form_ShangPinZiLiaoModi.Close; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.RecNo := CurrentNumber; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.Fields[3].FocusControl;
出现错误,错误内容是“cannot focus a disabled or invisible window”,
我应该怎么改呢?
谢谢!

 

“cannot focus a disabled or invisible window”错误的原因是DBGrid1所在的窗口还没有显示出来,操作某控件的Focus时其所在的窗口必须是已经显示出来的。所以你需要把
Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.RecNo := CurrentNumber; Form_KuCunGuanLi.DBGrid1.DataSource.DataSet.Fields[3].FocusControl;
放在窗口显示出来以后的操作中。
另外FocusControl方法是字段对象的方法,所有的数据集控件都应该是可以的。

 

窗口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.
还是改不好。我这个是MDI程序,MainForm中 Form1.ShowModal,Form1中Form2.ShowModal,Form1中有DBGrid,Form2是修改窗口,Form2修改之后
Form1.DBGrid1.Fields[3].FocusControl;出错。不用Form2直接在Form1中也不好用。
真是不好改。
你在Form2上做了什么操作后Form1.DBGrid1.Fields[3].FocusControl出的错?把代码贴上来看看比较好找出问题。
MainForm中:
procedure TMainForm.Button1Click(Sender: TObject);
begin
Form1.ShowModal;
end;

Form1中:
procedure TForm1.FormShow(Sender: TObject);
begin

try
if DataModule1.ADOConnection1.Connected then
begin
sql2 := select XuHao as 序号,zhengshuhao as 证书号,kuanhao as 款号, +
xiaojia as 销价,rukuliang as 入库量,zhiquanhao as 指圈号, +
shouhuoriqi as 收货日期,xiaoshouriqi as 销售日期,fanghuodanwei +
 as 放货单位,nid as ID from KuCunInfo order by xuhao;

DataModule1.ADOQuery1.Close;
DataModule1.ADOQuery1.SQL.Text := sql2;
DataModule1.ADOQuery1.ExecSQL ;
DataModule1.ADOQuery1.Active := true;
end;

finally
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
{修改KuCunInfo中的数据}

try
Form1.DBGrid1.Fields[3].FocusControl;
finally
end;
end;

这样就出错了,不用在Form2中修改。
哈哈,我弄出来了。在大富翁上搜索的。
Form1.DBGrid1.SelectedIndex := 4;
Form1.DBGrid1.SetFocus;
这样就行了。
谢谢你!
procedure TForm1.Button1Click(Sender: TObject);
begin
{修改KuCunInfo中的数据}
//修改完库存信息后,由于Form1中的数据还是旧的,所以需要在这里更新一下Form1中
//的数据,即:重新打开一次DBGrid1对应的数据集控件(DataModule1.ADOQuery1),然后 
//再执行FocusControl
try
Form1.DBGrid1.Fields[3].FocusControl;
finally
end;
end;

另外以下代码:
DataModule1.ADOQuery1.Close;
DataModule1.ADOQuery1.SQL.Text := sql2;
DataModule1.ADOQuery1.ExecSQL ;
DataModule1.ADOQuery1.Active := true;
建议这样写:
DataModule1.ADOQuery1.Close;
DataModule1.ADOQuery1.SQl.Clear;
DataModule1.ADOQuery1.SQL.Add(sql2);
DataModule1.ADOQuery1.Open;
ADOQuery1控件,如果其SQL为select语句使用Open,如果是Insert、update、delete等不需要返回值的SQL用ExecSQL 。

 

给DBGrid动态赋值后,如何用程序指定某行某列为当前焦点?(100分)

标签:send   ado   source   cli   数据集   inf   order   nal   dialog   

人气教程排行