Fórum Trocar o DataSource de Componentes em execução. #327577

17/08/2006

0

Ola amigos. Postei o problema de forma errada e estou retificando aqui.
Preciso mudar o Datasource de todos os Controles do Form em tempo de execução.
Consigo acessar as propriedades básicas de todos assim.
for I := 0 to Self.ComponentCount - 1 do
if Self.Components[I] is TControl then
if ((Self.Components[I] is TCustomEdit) or (Self.Components[I] is TCustomComboBox)) then TCustomControl(Self.Components[I]).OnEnter := ControleEnter;
Beleza para as propriedades de cor , fonte e etc.
Como faço para alterar o Datasource dos componentes , neste mesmo loop.
Temos 2 pré-soluções que pesquisei aqui no fórum
1: {------------------------------------------------------------------------------
Troca o DataModule de origem de todos os Datasets de ´AForm´ para o novo
´NewDataModule´
----------------------------------------------------------------------------- }
procedure MudaDataModule( AForm : TForm; NewDataModule : TDataModule ) ;
var
i:longint;
c: TComponent;
propInfo: PPropInfo;
l:longint;
ds: TDataSet;
dsname: string;
Ativo : Boolean ;
begin
for i:=0 to AForm.ComponentCount-1 do
begin
c := AForm.components[i]; // Look for the DataSource property
propInfo := getPropInfo(c.classinfo,´DataSet´);
if assigned(propinfo) then //If found
begin
l := getordprop(c,propInfo); //Gets the property
dsname := TDataSet(l).Name; //Gets the DataSet name
Ativo := TDataSet(l).Active ;
//And gets the instance of the new DataSet
ds := NewDataModule.findcomponent(dsname) as TDataSet ;
if assigned(ds) then
begin
TDataSet(l).Close ;
//Sets the new datasource
setordprop(c,propInfo,longint(ds));
l := getordprop(c,propInfo); //Gets the property
TDataSet(l).Active := Ativo ;
end;
end;
end;
end;

2: if [b:68cef5a07d]IsPublishedProp[/b:68cef5a07d](TCustomControl(Self.Components[I]), ´DataSet´) then
[b:68cef5a07d]SetPropValue[/b:68cef5a07d](TCustomControl(Self.Components[I]), ´DataSet´ ,((FormOrigem as TForm).FindComponent(´DSDetalhe´) as TDataSource));

A segunda opção é mais limpa , mas não consigo implementá-la.
Alguém sabe como fazê-lo ?
Falta declarar alguma unit em uses ?

Agradeço antecipadamente.


Emerson Azevedo

Emerson Azevedo

Responder

Posts

17/08/2006

Night_man

asisim a segunda opcao seria a melhor, q erro esta dando? tu declaro a unit TypInfo?

nao seria melhor tu trocar o dataset do datasource, soh teria q mudar em um lugar dai?


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar