请问如何设置dbgrid的列,我想通过设置来使有些列显示,有些列 |
答題得分者是:cashxin2002
|
heartsong
一般會員 發表:11 回覆:13 積分:4 註冊:2003-07-17 發送簡訊給我 |
|
cashxin2002
版主 發表:231 回覆:2555 積分:1937 註冊:2003-03-28 發送簡訊給我 |
您好﹗ 小弟做了一個有關這方面的範例﹐完整程式碼如下﹐您也可至此下載﹕
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=36615
============================================================================
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, CheckLst, Grids, DBGrids, DB, ADODB, Buttons, DBCommon; type TForm1 = class(TForm) ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; DBGrid1: TDBGrid; Memo1: TMemo; CheckListBox1: TCheckListBox; Label1: TLabel; Label2: TLabel; Button1: TButton; Button2: TButton; DataSource1: TDataSource; Memo2: TMemo; BitBtn1: TBitBtn; Label3: TLabel; Label4: TLabel; procedure Button1Click(Sender: TObject); procedure ADOQuery1AfterOpen(DataSet: TDataSet); procedure Button2Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); procedure FormActivate(Sender: TObject); procedure CheckListBox1Click(Sender: TObject); procedure ADOQuery1AfterClose(DataSet: TDataSet); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ADOQuery1.Close; ADOQuery1.SQL.Clear; if Memo1.Lines.Text <> '' then Try ADOQuery1.SQL.Add(Memo1.Lines.Text); ADOQuery1.Open; Except ShowMessage('SQL語法錯誤!'); end else begin Memo1.SetFocus; ShowMessage('無SQL查詢語法!') end; end; procedure TForm1.ADOQuery1AfterOpen(DataSet: TDataSet); var I,J : Integer; begin CheckListBox1.Items.Clear; For I:=0 To ADOQuery1.FieldCount-1 do CheckListBox1.Items.Add(ADOQuery1.Fields[I].FieldName); For J:=0 To CheckListBox1.Items.Count-1 do CheckListBox1.Checked[J] := True; Label4.Caption := GetTableNameFromSQL(ADOQuery1.SQL.Text); end; procedure TForm1.Button2Click(Sender: TObject); Var I : Integer; begin DBGrid1.Columns.Clear; For I:=0 To CheckListBox1.Items.Count-1 do begin DBGrid1.Columns.Add; DBGrid1.Columns[I].FieldName := CheckListBox1.Items.Strings[I]; if CheckListBox1.Checked[I] then DBGrid1.Columns[I].Visible := True else DBGrid1.Columns[I].Visible := False; end; end; procedure TForm1.BitBtn1Click(Sender: TObject); Var I : Integer; begin CheckListBox1.Items.Exchange(CheckListBox1.ItemIndex,0); DBGrid1.Columns.Clear; For I:=0 To CheckListBox1.Items.Count-1 do begin DBGrid1.Columns.Add; DBGrid1.Columns[I].FieldName := CheckListBox1.Items.Strings[I]; if CheckListBox1.Checked[I] then DBGrid1.Columns[I].Visible := True else DBGrid1.Columns[I].Visible := False; end; end; procedure TForm1.FormActivate(Sender: TObject); begin Memo1.SetFocus; Memo1.Lines.Text := 'Select * From 訂單'; Memo1.SelectAll; end; procedure TForm1.CheckListBox1Click(Sender: TObject); begin BitBtn1.Enabled := True; end; procedure TForm1.ADOQuery1AfterClose(DataSet: TDataSet); begin Label4.Caption := ''; CheckListBox1.Items.Clear; end; end.參考看看﹗ ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟 |
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
可以用
ado
Tadoquery+Tdatasource+Tdbgrid
或
bde
Tquery++Tdatasource+Tdbgrid 方法如下:
ADOQuery1.SQL.Clear ;
ADOQuery1.SQL.Text :='select field1,...,fieldn form mytable ...';
ADOQuery1.Open ; 设置:
DataSource1.DataSet :=adoquery1;
dbgrid1.DataSource :=DataSource1; 对于不同查询结果的现实,只需要将红色部分的sql改变就可以了
|
Miles
尊榮會員 發表:27 回覆:662 積分:622 註冊:2002-07-12 發送簡訊給我 |
|
heartsong
一般會員 發表:11 回覆:13 積分:4 註冊:2003-07-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |