delphi dbgrid数据行上下移动,不是光标的移动。例如我要点击下移,光标所在的数据集发生了就移动。急,急
开始数据行
1 22 444
2 33 555
3 44 gggg
如果光标在第二行,点击下一行则变成下面这种情况
1 22 444
2 44 gggg
3 33 555
第一个id为选择数据库表的行号。请明白了之后再回答,非常感谢 展开
2012-03-29
用索引可实现行移动
nit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGridEh, StdCtrls, ComCtrls, ExtCtrls, DB, DBClient,midasLib,
MConnect,shellapi;
type
TForm1 = class(TForm)
DBGrid: TDBGridEh;
Panel1: TPanel;
TreeView: TTreeView;
Panel2: TPanel;
btSave: TButton;
Button6: TButton;
Button7: TButton;
cds: TClientDataSet;
ds: TDataSource;
Button1: TButton;
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button6Click(Sender: TObject);
var
RowNo,newrow,olerow:integer;
begin
cds.CheckBrowseMode; //要先保存
RowNo:=cds.FieldByName('row_id').AsInteger;
With TClientDataSet.Create(nil) do begin
try
CloneCursor(cds,true);
DisableControls;
if Locate('row_id',RowNo,[])then begin
olerow:=FieldValues['Column_Site'];
RowNo:=FieldValues['row_id'];
Prior;
newrow:=FieldValues['Column_Site'];
Edit;
FieldValues['Column_Site']:=olerow;
CheckBrowseMode;
Next;
if Locate('row_id',RowNo,[]) then begin
Edit;
FieldValues['Column_Site']:=newrow;
End;
end;
EnableControls;
CheckBrowseMode;
finally
free;
end;
End;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
RowNo,newrow,olerow:integer;
begin
cds.CheckBrowseMode; //要先保存
RowNo:=cds.FieldByName('row_id').AsInteger;
With TClientDataSet.Create(nil) do begin
try
CloneCursor(cds,true);
DisableControls;
if Locate('row_id',RowNo,[])then begin
RowNo:=FieldValues['row_id'];
olerow:=FieldValues['Column_Site'];
Next;
newrow:=FieldValues['Column_Site'];
Edit;
FieldValues['Column_Site']:=olerow;
if Locate('row_id',RowNo,[]) then begin
Edit;
FieldValues['Column_Site']:=newrow;
End;
end;
EnableControls;
CheckBrowseMode;
finally
free;
end;
End;
cds.Next;
end;
varCopy2,varCopy3 : Variant;
totalFields : integer;
// 1. 创建长度
totalFields := q.fieldcount;
varcopy2 := varArrayCreate([0,totalFields-1],varVariant);
varcopy3 := varArrayCreate([0,totalFields-1],varVariant);
// 2. 取第二列,第三列的值
q.locate('num',2,[]); // 第二列
for i:= 0 to totalFields-1 do
varCopy2[i]:= q.fields[i].value;
q.locate('num',3,[]); // 第三列
for i:= 0 to totalFields-1 do
varCopy3[i]:= q.fields[i].value;
// 3. 开始编辑,第三列= varcopy2
q.edit;
for i:= 0 to totalFields-1 do
q.fields[i].value := varCopy2[i];
q.post;
// 第二列= varcopy3
q.locate('num',2,[]);
q.edit;
for i:= 0 to totalFields-1 do
q.fields[i].value := varCopy3[i];
q.post;
数据库中的表是不包含rownum的,rownum只是在显示时才有的。在q.post的时候报错了;错误如下
奇怪,试试看用这个query,
UPDATE
table AS t1
JOIN table AS t2 ON
( t1.item = 33 AND t2.item = 44 )
SET
t1.item = t2.item, // eg. 33 = 44
t2.item = t1.item,
t1.content = t2.content, // eg. gggg = 555
t2.content = t1.content