ItemDataBound no Datagrid do Delphi 2005

Veja neste artigo como usar o ItemDataBound no Datagrid do Delphi 2005

ItemDataBound no Datagrid do  Delphi 2005

Veja o arquivo pas como ficou.

unit WebForm1;

interface

uses
  System.Collections, System.ComponentModel,
  System.Data, System.Drawing, System.Web, System.Web.SessionState,
  System.Web.UI, System.Web.UI.WebControls, System.Web.UI.HtmlControls,
  Borland.Data.Provider;

type
  TWebForm1 =procedure InitializeComponent;{$REGION 'Designer Managed Code'}
  strict private
    procedure InitializeComponent;
    procedure DataGrid1_ItemDataBound(sender: System.Object; e: System.Web.UI.WebControls.DataGridItemEventArgs);
  {$ENDREGION}
  strict private
    proBdpConnection1: Borland.Data.Provider.BdpConnection;entArgBdpCommand1: Borland.Data.Provider.BdpCommand;orlandDataGrid1: System.Web.UI.WebControls.DataGrid;: Borland.Data.Provider.BdpCommand;
    DataGrid1: System.Web.UI.WebControls.DataGrid;
    procedure OnInit(e: EventArgs); override;
  private
    { Private Declarations }
  public
     procedure AbrirConsulta();
    { Public Declarations }
  end;

implementation

{$REGION 'Designer Managed Code'}
///
/// Required method for Designer support --procedure TWebForm1.InitializeComponent;
begin
  Self.BdpConnection1 := Borland.Data.Provider.BdpConnection.Create;
  Self.BdpCommand1 := Borland.Data.Provider.BdpCommand.Create;
  //
  // BdpConnection1
  //
  Self.BdpConnection1.ConnectionOptions := 'waitonlocks=False;commitretain=F' +
  'alse;sqldialect=3;transaction isolation=ReadCommitted;servercharset=;role' +
  'name=myrole';
  Self.BdpConnection1.ConnectionString := 'database=localhost:C:\Arquivos de' +
  ' programas\Borland\InterBase\examples\database\employee.gdb;assembly=Borl' +
  'and.Data.Interbase, Version=2.0.0.0, Culture=neutral, PublicKeyToken=91d6' +
  '2ebb5b0d1b1b;vendorclient=gds32.dll;provider=Interbase;username=sysdba;pa' +
  'ssword=masterkey';
  //
  // BdpCommand1
  //
  Self.BdpCommand1.CommandOptions := nil;
  Self.BdpCommand1.CommandText := 'SELECT EMP_NO, FIRST_NAME, LAST_NAME, SAL' +
  'ARY FROM EMPLOYEE order by FIRST_NAME';
  Self.BdpCommand1.CommandType := System.Data.CommandType.Text;
  Self.BdpCommand1.Connection := Self.BdpConnection1;
  Self.BdpCommand1.ParameterCount := (SmallInt(0));
  Self.BdpCommand1.SchemaName := nil;
  Self.BdpCommand1.Transaction := nil;
  Self.BdpCommand1.UpdatedRowSource := System.Data.UpdateRowSource.None;
  Include(Self.Load, Self.Page_Load);
end;ource := System.Data.UpdateRowSource.None;
  Include(Self.Load, Self.Page_Load);
end;
{$ENDREGION}

procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs);
begin
  // TODO: Put user code to initialize the page here
  if not IsPostBack then
    AbrirConsulta();
end;

procedure TWebForm1.OnInit(e: EventArgs);
begin
  //
  // Required for Designer support
  //
  InitializeComponent;
  inherited OnInit(e);
end;

procedure TWebForm1.AbrirConsulta;
var
  dr: BdpDataReader;
begin
  BdpConnection1.Open();
  try
    dr := BdpCommand1.ExecuteReader();
    DataGrid1.DataSource := dr;
    DataGrid1.DataBind();
  finally
    BdpConnection1.Close;
  end;
end;

procedure TWebForm1.DataGrid1_ItemDataBound(sender: System.Object;
  e: System.Web.UI.WebControls.DataGridItemEventArgs);
var
  Salario: System.Double;
  Lb: System.Web.UI.WebControls.Label;
begin
{  if ((e.Item.ItemIndex mod 2) = 0) then
    e.Item.BackColor := Color.Silver
  else
    e.Item.BackColor := Color.Gold;  }
  //  e.Item.Cells[3].BackColor := Color.Gold;
  for Lb in e.Item.Cells[3].Controls do
    if (Lb is System.Web.UI.WebControls.&Label) then
    begin
      Salario := Convert.ToDouble(Lb.Text);
      if Salario > 50000 then
      begin
         Lb.Font.Bold := true;
         Lb.ForeColor := Color.Red;
         E.Item.BackColor := Color.Gold;
      end;
    end;
end;

end.

Conforme exibe a figura abaixo, voce deve adicionar esta linha:
DataBinder.Eval(Container.DataItem,"SALARY") no DataBindings da Label1 a fim de exibir o conteudo do campo salario na label1



Até a próxima pessoal.

Artigos relacionados