1. 程式人生 > >DBGrid相關技術整理

DBGrid相關技術整理

DBGrid相關技術整理;

注:對於DBGrid相關屬性、方法的學習融入到技術整理過程中

一,多選

設定屬性:

  Options->dgMultiSelect = True;

           ->dgRowSelect   = True;

應用屬性:

  SelectedRows;

操作方法:

  -Ctrl + Mouse Clicks

  -Shift + Arrow Keys

For example:

  1,取得所選行某個欄位的值的合計:

  procedure TForm1.btnDoSumClick(Sender: TObject);

  var

    i: Integer;

    sum: Single;

  begin

    if DBGrid1.SelectedRows.Count > 0 then

    begin

      sum := 0;

      with DBGrid1.DataSource.DataSet do

      begin

        for i := 0 to DBGrid1.SelectedRows.Count - 1 do

begin

 GotoBookmard(Pointer(DBGrid1.SelectedRows.Items[i]));

 sum := sum + AdoQuery1.FieldByName('Size').asfloat;

end;

      end;

      edtSizeSum.Text := FloattoStr(sum);

    end;

  end;

  //用edtSizeSum來接收計算來的合計值

  2,全選DBGrid:

  procedure DBGridSelectAll(AGrid: TDBGrid) ;

  begin

    AGrid.SelectedRows.Clear;

    with AGrid.DataSource.DataSet do

    begin

      DisableControls;

      First;

      try

        while not EOF do

        begin

          AGrid.SelectedRows.CurrentRowSelected := True;

          Next;

        end;

      finally

        EnableControls;

      end;

    end;

  end;

深入瞭解:

  1,SelectedRows:

    The TDBGrid component keeps all the selections as Bookmarks in a TStringList, 

    and all the Bookmarks must be converted to a Pointer (what they really are) before using it.

    &

    The SelectedRows property is an object of type TBookmarkList.

    We can use the SelectedRows property to, for example:

    --get the number of rows selected,

    --clear the selection(unselect),

    --delete all the selected records,

    --check whether a particular record is selected.

  2,執行時設定DBGrid的多選屬性

    DBGrid1.Options := DBGrid1.Options + [dgMultiSelect];

//以上部分內容取自http://delphi.about.com/od/usedbvcl/l/aa032503a.htm


二,加入其它控制元件
http://delphi.about.com/od/usedbvcl/l/aa082003a.htm
http://delphi.about.com/od/usedbvcl/l/aa082003a.htm
三,顏色設定
http://delphi.about.com/od/usedbvcl/l/aa031699.htm