BMS-HOSxP Community
HOSxP => Delphi / Pascal => ข้อความที่เริ่มโดย: Bond_007 ที่ กันยายน 18, 2012, 09:03:08 AM
-
พอดีอยากทำฟอร์มให้ดึงค่าจาก combobox ครับ โดยผมเขียนคำสั่งแบบนี้ครับ แล้วเวลารันโปรแกรมหน้าตาจะเป็นดังรูปครับ
procedure TFormTest.FormShow(Sender: TObject);
var
str : string;
begin
Str := 'SELECT * FROM department ';
Query.Active := False;
Query.SQL.Clear;
Query.SQL.Add(Str);
Query.Active := True;
Query.First;
combobox1.Items.Clear;
Query.First;
while not Query.Eof do
begin
combobox1.Items.Add(Query.FieldByName('dept_name').AsString);
Query.Next;
end;
end;
end.
-
แต่ทีี่นี้เมื่อผมลองเลือกรายการมาสักหนึ่งรายการ เช่น test ผมอยากให้ dbgrid แสดงแค่ Test เฉยๆแบบนี่้ต้องทำยังไงครับ ขอบคุณครับ
-
ใช้ lookupcombobox น่าจะง่ายกว่านะครับ
เอา clientdataset กับ datasource มาแสดงเอาครับ
-
รูปที่ 2 บังโค้ด ตรงในส่วน FromShow อะครับ
-
code เป็นแบบนี้ครับ (ผมค้นมาจาก google) ครับแล้วมาปรับเอา ;D
procedure TFormTest.FormShow(Sender: TObject);
var
str : string;
begin
Str := 'SELECT * FROM department ';
Query.Active := False;
Query.SQL.Clear;
Query.SQL.Add(Str);
Query.Active := True;
Query.First;
combobox1.Items.Clear;
Query.First;
while not Query.Eof do
begin
combobox1.Items.Add(Query.FieldByName('dept_name').AsString);
Query.Next;
end;
end;
end.
รูปที่ 2 บังโค้ด ตรงในส่วน FromShow อะครับ
-
ขอบคุณครับ เด๋วผมขอลองทำดูครับ ไม่เคยเขียนกับ clientdataset ทุกทีใช้ myquery แต่จะพยายามทำให้ได้ครับ ;D
ใช้ lookupcombobox น่าจะง่ายกว่านะครับ
เอา clientdataset กับ datasource มาแสดงเอาครับ