⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridform.pas

📁 Delphi网络应用开发的源代码
💻 PAS
字号:
unit GridForm;

interface


uses
  System.Windows.Forms,
  System.ComponentModel,
  System.Drawing,
  System.Data,
  System.Data.SqlClient;

type
  TForm1 = class(Form)
  private
    components: System.ComponentModel.Container ;
    statusBar1 : System.Windows.Forms.StatusBar;
    // 定义DataSet
    customersDataSet1 : DataSet;
    buttonLoad : System.Windows.Forms.Button;
    // dataGrid控件用于显示数据
    dataGrid1: System.Windows.Forms.DataGrid;
  public
    // 构造函数
    constructor Create;
    procedure InitializeComponent;
    procedure Button1_Click( Sender : TObject; E : EventArgs );
  end;

var
  Form1: TForm1;

implementation


{ TForm1 }

// Button1按钮的点击事件响应函数,用于连接到数据库,并从数据库中读取数据
procedure TForm1.Button1_Click(Sender: TObject; E: EventArgs);
var
  con : SqlConnection;
  cmdCustomers : SqlDataAdapter ;

begin
  try
    //创建SqlConnection连接
    con := SqlConnection.Create('Data Source=(local);Trusted_Connection=yes;Initial Catalog=northwind');

    // 利用SqlConnection读取数据
    cmdCustomers := SqlDataAdapter.Create('Select * from Customers', con);
    statusBar1.Text :='载入顾客信息...';
    customersDataset1.Clear;

    cmdCustomers.Fill(customersDataSet1, 'Customers');
    dataGrid1.DataSource := customersDataset1;
    dataGrid1.DataMember := 'Customers';
  finally
    statusBar1.Text :='完成';
  end;
end;

constructor TForm1.Create;
begin
  inherited Create;
  InitializeComponent();
end;

// 创建和初始化各个控件
procedure TForm1.InitializeComponent;
var
  MyControls : array[0..2] of Control;
  ts : DataGridTableStyle;
begin
  Self.components := System.ComponentModel.Container.Create();
  Self.dataGrid1 := System.Windows.Forms.DataGrid.Create();
  Self.statusBar1 := System.Windows.Forms.StatusBar.Create();
  Self.customersDataSet1 := DataSet.Create();
  Self.buttonLoad := System.Windows.Forms.Button.Create();

  dataGrid1.BeginInit();

  if ( dataGrid1.TableStyles.Count = 0 ) then
  begin
    ts := DataGridTableStyle.Create();
    ts.MappingName:='Customers';
    dataGrid1.TableStyles.Add(ts);
  end;

  Self.dataGrid1.Text := '数据栅格';

  with dataGrid1.TableStyles['Customers'] do
    PreferredRowHeight := 16;
  Self.dataGrid1.TableStyles['Customers'].AlternatingBackColor := System.Drawing.Color.WhiteSmoke;
  Self.dataGrid1.Size := System.Drawing.Size.Create(500, 300);
  Self.dataGrid1.DataSource := customersDataSet1;
  Self.dataGrid1.ForeColor := System.Drawing.Color.Navy;
  Self.dataGrid1.TabIndex := 0;
  Self.dataGrid1.Anchor := AnchorStyles( Integer(AnchorStyles.Top) or
                                         Integer(AnchorStyles.Left) or
                                         Integer(AnchorStyles.Right));
  Self.dataGrid1.Location := System.Drawing.Point.Create(8, 8);
  Self.dataGrid1.BackColor := System.Drawing.Color.Gainsboro;


  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.Text := '顾客信息';
  Self.AcceptButton := buttonLoad as IButtonControl;
  Self.ClientSize := System.Drawing.Size.Create(516, 400);

  Self.statusBar1.BackColor := System.Drawing.SystemColors.Control;
  Self.statusBar1.Location := System.Drawing.Point.Create(0, 384);
  Self.statusBar1.Size := System.Drawing.Size.Create(516, 16);
  Self.statusBar1.TabIndex := 2;
  Self.statusBar1.Text := '载入数据';


  Self.buttonLoad.Anchor := AnchorStyles( Integer(AnchorStyles.Right) or Integer(AnchorStyles.Bottom));
  Self.buttonLoad.add_click(button1_Click);
  Self.buttonLoad.FlatStyle := System.Windows.Forms.FlatStyle.Flat;
  Self.buttonLoad.Location := System.Drawing.Point.Create(350, 350);
  Self.buttonLoad.Size := System.Drawing.Size.Create(112, 32);
  Self.buttonLoad.TabIndex := 1;
  Self.buttonLoad.Text := '载入数据';
  MyControls[0] := Self.statusBar1;
  MyControls[1] := Self.buttonLoad;
  MyControls[2] := dataGrid1;
  Self.Controls.AddRange(MyControls);
  Self.dataGrid1.EndInit();
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -