Recent

Author Topic: TDBCkeckBox  (Read 5662 times)

vks

  • New Member
  • *
  • Posts: 33
TDBCkeckBox
« on: March 03, 2015, 12:02:05 pm »
Hello,

I need to generate checkboxes in delphi based on the number of data in database. and the caption has to to given to checkbox as customized by us from a database field.

It works if there is one value which is being retrieved.
suppose its a table 'crncy' with 'country' and 'currency_of_country' as columns, and a SQL query like:
select country from currency where currency_of_country='yen';
 it retrieves Japan from database(i have stored it in database in prior).

I have code to display it to a TDBCheckBox component:


procedure Thome_trainee.btn_listClick(Sender: TObject);
var
  c : string;
begin
  SQLiteLibraryName:='sqlite3.dll';
  SQLite3Connection1.DatabaseName := 'testdbase.db';
  SQLite3Connection1.Transaction := SQLTransaction1;
  SQLTransaction1.DataBase := SQLite3Connection1;
  SQLite3Connection1.Connected:=true;
  SQLTransaction1.Database:=SQLite3Connection1;
  SQLQuery1.Database:=SQLite3Connection1;
  SQLQuery1.SQL.text:='Select country FROM crncy';
  SQLQuery1.open;

 DataSource1.DataSet:=SQLQuery1;
 c:= SQLQuery1.fieldbyname('country').AsString;
 DBCheckBox1.DataSource:=DataSource1;
 DBCheckBox1.Caption:=c;
end;


Problem is:
when i need to display array of string to different checkboxes as its caption, which is being generated dynamically,
(I know how to dynamically generate checkboxes but not its caption)  what do you suggest me to do?

flamer0n

  • Guest
Re: TDBCkeckBox
« Reply #1 on: March 03, 2015, 12:25:02 pm »
So you want to generate controls based on table data not on table structure right?

vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #2 on: March 03, 2015, 01:05:43 pm »
Ya exactly. What datatype is required to store the result of the SQLQuery? I mean the resultset.

flamer0n

  • Guest
Re: TDBCkeckBox
« Reply #3 on: March 03, 2015, 01:19:29 pm »
tdataset, but this question doesn't seem related to the previous one or maybe I misunderstood. I thought you wanted to generate controls based on the field.Value , I imagined some csv string content in a field and you trying to split to stringArray and generate controls looping through that assigning string values to captions etc.. nevermind

vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #4 on: March 03, 2015, 02:00:55 pm »
That was an extended question about resultset.
I need to generate checkboxes with its label as in database.
if database table 'crncy' is like below:

c_no(PK)   |   country     |   currency
---------------------------------------
1                |   India          |   Rupee
2                |   US             |   USD
3                |   Japan         |   Yen           

when i give:    select country from crncy;

3 checkboxes must be generated with its label  --- India,US,Yen respectively.

to store the result of the query, do we have any data type like 'string array', so that i can then assign values
to the checkboxes within a 'for loop' after this query execution??


« Last Edit: March 04, 2015, 07:49:47 am by vks »

vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #5 on: March 03, 2015, 02:02:56 pm »
oh ok.. tdataset will store the query result?
thanks a lot..   :) :)

will come back to you after trying this.. :) or if you have better solutions, please post..
« Last Edit: March 03, 2015, 02:07:19 pm by vks »

flamer0n

  • Guest
Re: TDBCkeckBox
« Reply #6 on: March 03, 2015, 02:07:57 pm »
I think you want to access the result returned from the sqlquery right? it's in the query's dataset. 

read data from the query like this
Code: [Select]
query.Open;
while not query.Eof do
begin
 {Generate your checkboxes and assign captions yourCheckBox.Caption := query.FieldByName('India').AsString}
 query.Next;
end;


vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #7 on: March 04, 2015, 07:48:39 am »
Hello..

I tried your code..its working super fine.. thanq so much.. here is what i have written:

Code: [Select]
N:=DataSource1.DataSet.RecordCount;
   for I:=1 to N do
   begin
   DBCheckBox:=TDBCheckBox.Create(Self);
   DBCheckBox.DataSource:=DataSource1;
   DBCheckBox.Visible:=False;
   CheckBox:=TCheckBox.Create (Self);
   CheckBox.Parent := ts_enrol;
   CheckBox.Caption := SQLQuery1.FieldByName('Country').AsString;
   CheckBox.Top := Top;
   CheckBox.Width:=400;
   CheckBox.Left := 86 + I*86;
   SQLQuery1.Next;
   end 
« Last Edit: March 04, 2015, 08:27:17 am by vks »

vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #8 on: March 04, 2015, 08:29:47 am »
The generated checkboxes appear one after the other..(horizontally)
I need vertical arrangement. Which property to set?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TDBCkeckBox
« Reply #9 on: March 04, 2015, 10:04:25 am »
Instead of giving each checkbox a fixed Top, and varying its Left, give each checkbox a fixed Left, and vary its Top property suitably.

vks

  • New Member
  • *
  • Posts: 33
Re: TDBCkeckBox
« Reply #10 on: March 04, 2015, 10:50:59 am »
@flamerOn @howardpc.. thank you so much :-) ^_^

 

TinyPortal © 2005-2018