DBGrid是Delphi里显示数据的主要组件之一,不过很多地方都非常不得心应手,而昨天终于找到了一种比较好的解决方案。
先说继承关系,TObject->TPersistent->TComponent->TControl->TWinControl->TCustomControl->TCustomGrid->TCustomDBGrid->TDBGrid
其中TCustomDBGrid->TDBGrid这里TDBGrid隐藏了TCustomDBGrid的几个属性和方法,现在看来,这几个东西都是非常重要的,主要包括Row,Col,SelectedRows等,所以要通过手动继承这个类来实现(原理不详-_-):
type
NBGrid = class(TCustomDBGrid)//NB的Grid-_-
end;
于是行列号的问题迎刃而解,NBGrid(DBGrid1).Row和NBGrid(DBGrid1).Col是当前行列号,RowCount和ColCount是行列总数。
行选取的问题要复杂一些,本来公司有一个控件叫MyDBGrid,提供了RowSelect等功能,不过有一个Bug,就是在RowSelectMode为rsAdvanced的时,通过其它方法向里边插入数据时会出莫名其妙的错误,于是只好不用。用上面的方法就可以解决了,要将DBGrid的Option里的RowSelect和MultiSelect选上。
下面的代码为将DBGrid1中的选中条数据移动到DBGrid2中的方法,其中两个DBGrid的TTable必须有完全相同的字段,且有一个表示Check的字段。
献丑了(MoveRecord和SetField方法比较简单,且不同的程序需要不同的判断条件,故忽略了):
procedure TProcFQ.MoveGrid(dg1, dg2: NBGrid);
var
i, ct: Integer;
t1, t2: TTable;
begin
t1 := TTable(dg1.DataSource.DataSet);
t2 := TTable(dg2.DataSource.DataSet);
if t1.RecordCount > 0 then
begin
if dg1.SelectedRows.Count > 1 then
begin
ct := t1.RecordCount;
i := 1;
while i < ct do
begin
t1.RecNo := i;
if dg1.SelectedRows.CurrentRowSelected then
if MoveRecord(t1, t2) then
SetField(t1.Fields[3], 1);
Inc(i);
end;
i := 1;
while i < ct do
begin
t1.RecNo := i;
if t1.Fields[3].AsInteger = 1 then
begin
t1.Delete;
dec(i);
end;
Inc(i);
end;
t1.First;
end
else
begin
if MoveRecord(t1, t2) then
t1.Delete;
end;
end;
dg2.SelectedRows.Clear;
end;
先说继承关系,TObject->TPersistent->TComponent->TControl->TWinControl->TCustomControl->TCustomGrid->TCustomDBGrid->TDBGrid
其中TCustomDBGrid->TDBGrid这里TDBGrid隐藏了TCustomDBGrid的几个属性和方法,现在看来,这几个东西都是非常重要的,主要包括Row,Col,SelectedRows等,所以要通过手动继承这个类来实现(原理不详-_-):
type
NBGrid = class(TCustomDBGrid)//NB的Grid-_-
end;
于是行列号的问题迎刃而解,NBGrid(DBGrid1).Row和NBGrid(DBGrid1).Col是当前行列号,RowCount和ColCount是行列总数。
行选取的问题要复杂一些,本来公司有一个控件叫MyDBGrid,提供了RowSelect等功能,不过有一个Bug,就是在RowSelectMode为rsAdvanced的时,通过其它方法向里边插入数据时会出莫名其妙的错误,于是只好不用。用上面的方法就可以解决了,要将DBGrid的Option里的RowSelect和MultiSelect选上。
下面的代码为将DBGrid1中的选中条数据移动到DBGrid2中的方法,其中两个DBGrid的TTable必须有完全相同的字段,且有一个表示Check的字段。
献丑了(MoveRecord和SetField方法比较简单,且不同的程序需要不同的判断条件,故忽略了):
procedure TProcFQ.MoveGrid(dg1, dg2: NBGrid);
var
i, ct: Integer;
t1, t2: TTable;
begin
t1 := TTable(dg1.DataSource.DataSet);
t2 := TTable(dg2.DataSource.DataSet);
if t1.RecordCount > 0 then
begin
if dg1.SelectedRows.Count > 1 then
begin
ct := t1.RecordCount;
i := 1;
while i < ct do
begin
t1.RecNo := i;
if dg1.SelectedRows.CurrentRowSelected then
if MoveRecord(t1, t2) then
SetField(t1.Fields[3], 1);
Inc(i);
end;
i := 1;
while i < ct do
begin
t1.RecNo := i;
if t1.Fields[3].AsInteger = 1 then
begin
t1.Delete;
dec(i);
end;
Inc(i);
end;
t1.First;
end
else
begin
if MoveRecord(t1, t2) then
t1.Delete;
end;
end;
dg2.SelectedRows.Clear;
end;
插入表情