dbgrideh实现多列选中

本文介绍了一个使用Delphi进行数据库操作的应用实例,通过ADO组件连接数据库,并实现了基于DBGridEh的数据浏览及批量修改功能。具体展示了如何通过鼠标操作实现对特定时间段内数据的选择与反转状态。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GridsEh, DBGridEh, Db, ADODB, StdCtrls,math;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    DBGridEh1: TDBGridEh;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure DBGridEh1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure DBGridEh1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  x1,x2,y1,y2,x3,y3:integer;
  col1,row1,col2,row2:integer;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DBGridEh1.DataSource:=DataSource1;
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select SampleNO as 样本人编号 ,BeginTime as 开始时间,EndTime as 结束时间,Monday as 周一,Tuesday as 周二,Wednesday as 周三,Thursday as 周四,Friday as 周五,Saturday as 周六,Sunday as 周日 ');
    sql.Add('from  Sample_Set');
    sql.Add('where AreaCode= '#39+'131100'+#39' and SampleNO = '#39+'131100000803'+#39'');
    sql.Add(' order by BeginTime');
    open;
  end;

end;

procedure TForm1.DBGridEh1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

var   p:   TPoint;
   i,hengbei,rowbei:integer;
begin
    p:=   Point(X,   Y);
    //172是行的复选框的第一个开始
    hengbei:=ceil((x-172)/34);

    if hengbei=1 then
    begin
      col1:=3;
    end;
    if hengbei=2 then
    begin
      col1:=4;
    end;
    if hengbei=3 then
    begin
      col1:=5;
    end;
    if hengbei=4 then
    begin
      col1:=6;
    end;
    if hengbei=5 then
    begin
      col1:=7;
    end;
    if hengbei=6 then
    begin
      col1:=8;
    end;
    if hengbei=7 then
    begin
      col1:=9;
    end;
    rowbei:=ceil((y-20)/20);
    if rowbei=1 then
    begin
      row1:=1;
    end;
    if rowbei=1 then
    begin
      row1:=1;
    end;
    if rowbei=2 then
    begin
      row1:=2;
    end;
    if rowbei=3 then
    begin
      row1:=3;
    end;
    if rowbei=4 then
    begin
      row1:=4;
    end;if rowbei=5 then
    begin
      row1:=5;
    end;
    if rowbei=6 then
    begin
      row1:=6;
    end;
    if rowbei=7 then
    begin
      row1:=7;
    end;
    if rowbei=8 then
    begin
      row1:=8;
    end;
    if rowbei=9 then
    begin
      row1:=9;
    end;
    if rowbei=10 then
    begin
      row1:=10;
    end;
    if rowbei=11 then
    begin
      row1:=11;
    end;
    if rowbei=12 then
    begin
      row1:=12;
    end;
    if rowbei=13 then
    begin
      row1:=13;
    end;
    if rowbei=14 then
    begin
      row1:=14;
    end;
    if rowbei=15 then
    begin
      row1:=15;
    end;
    if rowbei=16 then
    begin
      row1:=16;
    end;
    if rowbei=17 then
    begin
      row1:=17;
    end;
    if rowbei=18 then
    begin
      row1:=18;
    end;


    x1:=x;
    y1:=y;


    x3:=x+34*7;
    y3:=y+20*18

end;

 

procedure TForm1.DBGridEh1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  hengbei2,rowbei2:integer;
begin
    {
    //鼠标抬起的位置
    //x2:=Mouse.CursorPos.X;
    //y2:=Mouse.CursorPos.Y;
     x2:=x;
     y2:=y;

    //showmessage(inttostr(x2)+' '+inttostr(y2));
    }

    hengbei2:=ceil((x-172)/34);

    if hengbei2=1 then
    begin
      col2:=3;
    end;
    if hengbei2=2 then
    begin
      col2:=4;
    end;
    if hengbei2=3 then
    begin
      col2:=5;
    end;
    if hengbei2=4 then
    begin
      col2:=6;
    end;
    if hengbei2=5 then
    begin
      col2:=7;
    end;
    if hengbei2=6 then
    begin
      col2:=8;
    end;
    if hengbei2=7 then
    begin
      col2:=9;
    end;
    rowbei2:=ceil((y-20)/20);
    if rowbei2=1 then
    begin
       row2:=1;
    end;
    if rowbei2=1 then
    begin
       row2:=1;
    end;
    if rowbei2=2 then
    begin
       row2:=2;
    end;
    if rowbei2=3 then
    begin
       row2:=3;
    end;
    if rowbei2=4 then
    begin
       row2:=4;
    end;if rowbei2=5 then
    begin
       row2:=5;
    end;
    if rowbei2=6 then
    begin
       row2:=6;
    end;
    if rowbei2=7 then
    begin
       row2:=7;
    end;
    if rowbei2=8 then
    begin
       row2:=8;
    end;
    if rowbei2=9 then
    begin
       row2:=9;
    end;
    if rowbei2=10 then
    begin
       row2:=10;
    end;
    if rowbei2=11 then
    begin
       row2:=11;
    end;
    if rowbei2=12 then
    begin
       row2:=12;
    end;
    if rowbei2=13 then
    begin
       row2:=13;
    end;
    if rowbei2=14 then
    begin
       row2:=14;
    end;
    if rowbei2=15 then
    begin
       row2:=15;
    end;
    if rowbei2=16 then
    begin
       row2:=16;
    end;
    if rowbei2=17 then
    begin
       row2:=17;
    end;
    if rowbei2=18 then
    begin
       row2:=18;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i,j,m:integer;
  rrect:trect;
begin
  showmessage('鼠标落下的行列'+inttostr(row1)+' '+inttostr(col1)+'鼠标抬起的行列'+inttostr(row2)+' '+inttostr(col2));
  DBGridEh1.DataSource.DataSet.First;
  {for i:=0 to DBGridEh1.DataSource.DataSet.RecordCount-1 do
  begin
    for j:=0 to 5 do
    begin
    showmessage(DBGridEh1.Fields[j].asvariant);
    end;
    showmessage(DBGridEh1.Fields[1].asvariant);
    DBGridEh1.DataSource.DataSet.MoveBy(1);
  end;}
  DBGridEh1.DataSource.DataSet.First;
  DBGridEh1.DataSource.DataSet.DisableControls ;
  DBGridEh1.DataSource.DataSet.Edit;
  if row2>=row1 then
  begin
    //从头开始走
    for m:=1 to DBGridEh1.DataSource.DataSet.RecordCount do
    begin
      if (m>=row1) and (m<=row2) then
      begin
          DBGridEh1.DataSource.DataSet.Edit;
          for i:=col1 to col2 do
          begin
            if DBGridEh1.Fields[i].AsVariant=0  then
            DBGridEh1.Fields[i].AsVariant:=1
            else
            DBGridEh1.Fields[i].AsVariant:=0;
            //showmessage(DBGridEh1.Fields[i].asvariant);
         end;
         //showmessage('sdf');
         //showmessage(DBGridEh1.Fields[j].asvariant);
      end
      else
      begin
        //continue;
      end;
      DBGridEh1.DataSource.DataSet.MoveBy(1);
    end; //for m:=1 to DBGridEh1.DataSource.DataSet.RecordCount do
  end //if row2>row1 then
  else
  begin
    showmessage('拖动的次序不对');

    exit;

  end;
 DBGridEh1.DataSource.DataSet.EnableControls;
    {
  DBGridEh1.DataSource.DataSet.DisableControls ;
  DBGridEh1.DataSource.DataSet.Edit;
  //从头开始走
  for m:=0 to DBGridEh1.DataSource.DataSet.RecordCount-1 do
  begin
    if (m>=row1) and (m<=row2) then
    begin
         for j:=row1 to row2 do
         begin
            DBGridEh1.DataSource.DataSet.Edit;
            for i:=col1 to col2 do
            begin
              if DBGridEh1.Fields[i].AsVariant=0  then
              DBGridEh1.Fields[i].AsVariant:=1
              else
              DBGridEh1.Fields[i].AsVariant:=0;
              //showmessage(DBGridEh1.Fields[i].asvariant);
           end;
           //showmessage('sdf');
           //showmessage(DBGridEh1.Fields[j].asvariant);


         end;
    end
    else
    begin
      continue;
    end;
    DBGridEh1.DataSource.DataSet.MoveBy(1);
  end;
  DBGridEh1.DataSource.DataSet.EnableControls;
  }

 

 

end;

e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值