dbGrid中的用法参考估计一样 选取的行都存在dbgrid1.selectrows中 for I;=0 to dbgrid1.selectrows.count-1 do with Dbgrid1.datasource.dataset do begin gotobook(Pointer(DBgrid1.selectedrows[i])) Memo1.lines.add(FieldByName('Name').asstring; end;
不能以上的方法! 我写了一个: function GetSelectedDataByRow(DBGEh: TDBGridEh; QryDest: TQuery; ColName: string; ColSize: Integer; ValName: string; FixedCols: TStrings): Boolean; function GetFieldSQLs: string; function GetFieldSQL(FieldName: string): string; var tmp: TField; n: Integer; begin tmp:=DBGEh.DataSource.DataSet.FindField(FieldName); Result:='['+FieldName+']='; if tmp=nil then begin Result:=Result+''''+StrN(' ', 100)+''''; Exit; end; case tmp.DataType of ftDateTime: Result:=Result+'GetDate()'; ftFloat, ftCurrency: Result:=Result+'9999999999.999'; ftSmallInt, ftInteger, ftWord: Result:=Result+'9999999999'; ftBoolean: Result:=Result+'CONVERT(BIT,0)';//仅SQL Server中有用 else begin n:=tmp.Size; if n<=0 then n:=30; Result:=Result+''''+StrN(' ', n)+''''; end; end; end; var i: Integer; tmpStr: string; begin Result:=''; if FixedCols<>nil then for i:=0 to FixedCols.Count-1 do begin tmpStr:=GetFieldSQL(FixedCols[i]); if tmpStr='' then Continue; if i=0 then Result:='SELECT '+tmpStr else Result:=Result+', '+tmpStr; end; if (Result<>'') and (ColName<>'') then begin Result:=Result+', ['+ColName+']='''+StrN(' ', ColSize)+''''; Result:=Result+', ['+ColName+'_D]='''+StrN(' ', ColSize)+''''; Result:=Result+', ['+ValName+']=999999999.99'; end; end; procedure WriteRecord(ColList: TColumnsEhList); var i, j: Integer; tmp: string; begin with DBGEh.DataSource.DataSet do begin for i:=0 to ColList.Count-1 do begin QryDest.Append; for j:=FixedCols.Count-1 downto 0 do begin tmp:=FixedCols[j]; if FindField(tmp)=nil then QryDest[tmp]:='' else QryDest[tmp]:=FieldByName(tmp).AsString; end; tmp:=ColList[i].FieldName; QryDest[ColName]:=tmp; QryDest[ColName+'_D']:=ColList[i].Title.Caption; if FindField(tmp)=nil then QryDest[ValName]:=0 else if FieldByName(tmp).DataType in [ftFloat, ftInteger, ftSmallInt, ftWord, ftAutoInc, ftCurrency] then QryDest[ValName]:=FieldByName(tmp).AsFloat else QryDest[ValName]:=0; QryDest.Post; end; end; end; var tmpStr: string; i: Integer; ColList: TColumnsEhList; ASelectionType: TDBGridEhSelectionType; begin Result:=False; QryDest.SQL.Clear; tmpStr:=GetFieldSQLs; if tmpStr='' then Exit; QryDest.SQL.Add(GetFieldSQLs); QryDest.Active:=True; while not QryDest.Eof do QryDest.Delete; ASelectionType:=DBGEh.Selection.SelectionType; if ASelectionType = gstNon then Exit; with DBGEh do begin with DataSource.Dataset do begin DisableControls; SaveBook; try case ASelectionType of gstRecordBooks: begin ColList := VisibleColumns; for i := 0 to Selection.Rows.Count-1 do begin Book := Selection.Rows[I]; WriteRecord(ColList); end; end; gstRectangle: begin ColList := TColumnsEhList.Create; try for i := Selection.Rect.LeftCol to Selection.Rect.RightCol do if Columns[i].Visible then ColList.Add(Columns[i]); Book := Selection.Rect.TopRow; while True do begin WriteRecord(ColList); if CompareBooks(Pointer(Selection.Rect.BottomRow),Pointer(Book)) = 0 then Break; Next; if Eof then Break; end; finally ColList.Free; end; end; gstColumns: begin ColList := Selection.Columns; First; while Eof = False do begin WriteRecord(ColList); Next; end; end; gstAll: begin ColList := VisibleColumns; First; while Eof = False do begin WriteRecord(ColList); Next; end; end; end; finally RestoreBook; EnableControls; end; end; end; Result:=True; end;
选取的行都存在dbgrid1.selectrows中
for I;=0 to dbgrid1.selectrows.count-1 do
with Dbgrid1.datasource.dataset do
begin
gotobook(Pointer(DBgrid1.selectedrows[i]))
Memo1.lines.add(FieldByName('Name').asstring;
end;
我写了一个:
function GetSelectedDataByRow(DBGEh: TDBGridEh; QryDest: TQuery;
ColName: string; ColSize: Integer; ValName: string; FixedCols: TStrings): Boolean;
function GetFieldSQLs: string;
function GetFieldSQL(FieldName: string): string;
var
tmp: TField;
n: Integer;
begin
tmp:=DBGEh.DataSource.DataSet.FindField(FieldName);
Result:='['+FieldName+']=';
if tmp=nil then
begin
Result:=Result+''''+StrN(' ', 100)+'''';
Exit;
end;
case tmp.DataType of
ftDateTime:
Result:=Result+'GetDate()';
ftFloat, ftCurrency:
Result:=Result+'9999999999.999';
ftSmallInt, ftInteger, ftWord:
Result:=Result+'9999999999';
ftBoolean:
Result:=Result+'CONVERT(BIT,0)';//仅SQL Server中有用
else
begin
n:=tmp.Size;
if n<=0 then n:=30;
Result:=Result+''''+StrN(' ', n)+'''';
end;
end;
end;
var
i: Integer;
tmpStr: string;
begin
Result:='';
if FixedCols<>nil then
for i:=0 to FixedCols.Count-1 do
begin
tmpStr:=GetFieldSQL(FixedCols[i]);
if tmpStr='' then Continue;
if i=0 then
Result:='SELECT '+tmpStr
else
Result:=Result+', '+tmpStr;
end;
if (Result<>'') and (ColName<>'') then
begin
Result:=Result+', ['+ColName+']='''+StrN(' ', ColSize)+'''';
Result:=Result+', ['+ColName+'_D]='''+StrN(' ', ColSize)+'''';
Result:=Result+', ['+ValName+']=999999999.99';
end;
end;
procedure WriteRecord(ColList: TColumnsEhList);
var
i, j: Integer;
tmp: string;
begin
with DBGEh.DataSource.DataSet do
begin
for i:=0 to ColList.Count-1 do
begin
QryDest.Append;
for j:=FixedCols.Count-1 downto 0 do
begin
tmp:=FixedCols[j];
if FindField(tmp)=nil then
QryDest[tmp]:=''
else
QryDest[tmp]:=FieldByName(tmp).AsString;
end;
tmp:=ColList[i].FieldName;
QryDest[ColName]:=tmp;
QryDest[ColName+'_D']:=ColList[i].Title.Caption;
if FindField(tmp)=nil then
QryDest[ValName]:=0
else if FieldByName(tmp).DataType in [ftFloat, ftInteger, ftSmallInt, ftWord, ftAutoInc, ftCurrency] then
QryDest[ValName]:=FieldByName(tmp).AsFloat
else
QryDest[ValName]:=0;
QryDest.Post;
end;
end;
end;
var
tmpStr: string;
i: Integer;
ColList: TColumnsEhList;
ASelectionType: TDBGridEhSelectionType;
begin
Result:=False;
QryDest.SQL.Clear;
tmpStr:=GetFieldSQLs;
if tmpStr='' then Exit;
QryDest.SQL.Add(GetFieldSQLs);
QryDest.Active:=True;
while not QryDest.Eof do
QryDest.Delete;
ASelectionType:=DBGEh.Selection.SelectionType;
if ASelectionType = gstNon then Exit;
with DBGEh do
begin
with DataSource.Dataset do
begin
DisableControls;
SaveBook;
try
case ASelectionType of
gstRecordBooks:
begin
ColList := VisibleColumns;
for i := 0 to Selection.Rows.Count-1 do
begin
Book := Selection.Rows[I];
WriteRecord(ColList);
end;
end;
gstRectangle:
begin
ColList := TColumnsEhList.Create;
try
for i := Selection.Rect.LeftCol to Selection.Rect.RightCol do
if Columns[i].Visible then
ColList.Add(Columns[i]);
Book := Selection.Rect.TopRow;
while True do
begin
WriteRecord(ColList);
if CompareBooks(Pointer(Selection.Rect.BottomRow),Pointer(Book)) = 0 then Break;
Next;
if Eof then Break;
end;
finally
ColList.Free;
end;
end;
gstColumns:
begin
ColList := Selection.Columns;
First;
while Eof = False do
begin
WriteRecord(ColList);
Next;
end;
end;
gstAll:
begin
ColList := VisibleColumns;
First;
while Eof = False do
begin
WriteRecord(ColList);
Next;
end;
end;
end;
finally
RestoreBook;
EnableControls;
end;
end;
end;
Result:=True;
end;
有三种:框选一部门,单击选择N行,单击选择N列。gstRecordBooks块是你要的。
自己看吧。
gstRecordBooks:
begin
ColList := VisibleColumns;
for i := 0 to Selection.Rows.Count-1 do
begin
Book := Selection.Rows[I];
end;
end;