Link to home
Start Free TrialLog in
Avatar of ibrobar
ibrobar

asked on

Delphi - DataSet and Text files

Hi

How can i load table fields / data from text file . and how can i save it to the file
thanks
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

if you use ADO the easiest way is use built in functionality for XML:

ADOQuery1.SaveToFile('c:\mydata.xml', pfXML);
ADOQuery1.LoadFromFile('c:\mydata.xml');

but if you prefer other DB controls or dont want xml then
loop thru all fields and all records and save to .csv file


procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  while not datasource.EOF do begin
    for i := 0 to datasource.fieldscount - 1 do
      //
    datasource.next;
  end;
end;

ziolko.
Avatar of ibrobar
ibrobar

ASKER

Hi zioloko

Thanks for your help but could please write me the code . and i don't know about Ado
Cann't we use like an excel file or simple text file

thanks , regads
>>Cann't we use like an excel file or simple text file
it's impossible to write one piece of code to read from any file
there are completely different methods to read excel files, different for csv files...
so if you want some working piece of code tell me what is file's format

ziolko.

Avatar of ibrobar

ASKER

Dear Ziolko

I am using simple text file to get TreeView nodes but afcourse when i am closing the application I am losing any changes i am doing to the tree that's why i want to save my text file to a database.
I know that i can load the tree nodes from a database directly without using file, but i want to give the user of the application the ability to open a file and to creat his own tree simply .
so how can i do this

thanks
ahhh now it's a lot easier to help you:)
in your Q you wrote about fields and data so I thought
you want to save some database table to file:)

ok, if you want to store structure of TreeView in textfile first thing you
need to do is decide what format use, for example:
node_id parentnode_id node_caption
1            -1                     root1
2            -1                     root2
3            1                      1st child of root1
4            1                      2nd child of root1  
5            2                      1st child of root2
.
.
.

is this way is good for you I can paste here some working code,
just let me know

ziolko.
Avatar of ibrobar

ASKER

Hi Ziolko

let's say i am using as simple as notepad, and i am filling the text file with my tree nodes (it's easy to do).
Up to now everything is good , but my problem is to save this file to a database so i won't lose the changes i am doing to the tree during the runtime.
For example if i remove a node or i add a new one . if i logged out my program and then logged in again
I will lose any changes and i will get the same tree again. so what i want to do is when i do changes either i want to save it to database and then load it again from the database or if it is possible to save the tree after the modification to the file it self.

regards
i'm confused... correct me if i'm wrong.
1. you have treeview
2. you make changes to treeview (add/remove/move treenodes)
3. you save state of treeview to file
4. you want save file (created in point 3) to database
and then when you start your app next time
1. read file from database
2. read treeview state from file
3. restore treeview state to what you read form file

is that right?

ziolko.
Avatar of ibrobar

ASKER

Hi ziolko

Let me explain to you :  I have a treeView
1. First I want to load it's nodes from a file.
2. Second I am doing changes to the nodes like add / reomve and edit
3. before i leave my application i want to store these changes to the same file. means i want to save treeview to a file.
Is it possible ............ ??

P.S : in my application i am giving the users the possibilty to load a default tree which i loaded from a database.
And the other option is to creat his own tree from a text file cause it looks easy like using word and typing so he open a text file write his chosen tree and then loaded to the tree which i can do it .
up to here it's good but now when any changes i want it to restore it back to the file . afcourse i can save it to a database and load again from it but here i will get back to the first solution which i don't want unless my demand cann't be done.

thanks a lot
sure it can be done.
just one more question what data should be loaded into tree,
except tree structure and captions?

ziolko
Avatar of ibrobar

ASKER

Hi zoliko

sorry for the delay , i was in a holiday.
What Data I am loading to the tree is just the years and the months
my tree takes the following shape :
Library
           2000
                   jan
                  feb
                   ..........
                 dec
           2001
           2002
and so on.
that's it
ok, i got it
i'll post working code.. maybe not tomorrow but on weekend for sure

so how was your holiday? are you ready for new lines of code?:)

ziolko.
Avatar of ibrobar

ASKER

Hi Zoilko

Thanks for your help , I enjoyed the holiday afcourse , although i was sitting on my pc for long times

regards
Avatar of ibrobar

ASKER

Hi zoliko I am stil waiting for your code.

thanks a lot
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ibrobar

ASKER

hI ZIOLKO

Thanks for your help . i will try it today.
can you help with this , I am asking everywhere but no body is answering me , why I don't know ? is it that difficult . help me with it and  i will double the point.
I need to add icons to combobox to the left of the text. I know that i have to use the ondrawitem and canvas but it's more complicated afcourse.
thanks a lot
Avatar of ibrobar

ASKER

Hi ziolko

I have a comment on your solution .
Cann't we simply use these two commands :

TreeView.LoadFromFile('FileName');  And
TreeView.SaveFromFile('FileName')

thanks

heh funny:)
TreeView.LoadFromFile and TreeView.SaveToFile  
look exactly as i did it:)

yup, you need set ComboBox.Style to csOwnerDrawFixed or csOwnerDrawVariable
and then use OnDrawItem()

i never did it with TComboBox but i shouldn't be as complicated:)

ziolko.
Avatar of ibrobar

ASKER

Hi ziolko

So i'd rather use the treeview.save and load from file cause it's shorter , right :)
Anyway thanks a lot
sure, no problem:)
and about that combobox i'll see what i can do:)
ziolko.
here's some code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBox1.Items.Add('ala');
  ComboBox1.Items.Add('ola');
  ComboBox1.Items.Add('ela');
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect:
    TRect; State: TOwnerDrawState);
var AText: string;
    pic: TGraphic;
begin
  pic := Image1.Picture.Icon;
  AText := ComboBox1.Items[Index];
  ComboBox1.Canvas.Draw(Rect.Left, Rect.Top, pic);
  InflateRect(Rect, -8, 0);
  OffsetRect(Rect, 8, 0);
  ComboBox1.Canvas.FillRect(Rect);
  DrawTextEx(ComboBox1.Canvas.Handle, PChar(AText), Length(AText), Rect,
              DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOCLIP, nil );
end;

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var
    Height: Integer);
begin
  Height := 16;
end;

Image1 contains 16X16 icon

ziolko.
Avatar of ibrobar

ASKER

Hi ziolko

I will check and let you know

thanks
Avatar of ibrobar

ASKER

Hi ziolko

I tried the code and it does draw an icon to the left of the text in a CombBox but it's not so accurate.
It's coming over the text and its big the 16*16.
what can i do in this case.
thanks
just change this values:

  InflateRect(Rect, -8, 0);
  OffsetRect(Rect, 8, 0);

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var
    Height: Integer);
begin
  Height := 16;
end;

untill you get desired effect

ziolko.
Avatar of ibrobar

ASKER

Hi

I tried this yesterday , but it didn't work like i want, maybe the reason is the icon i am using.
Can we use other than 16*16 icon.
What if i have smaller or bigger should i change the height in measureItem procedure.

regards
yup, you have to change height in on measure item and both InflateRect() and OffestRect()

ziolko.
is this Q still open?

please see here:
https://www.experts-exchange.com/help.jsp#hs5

ziolko.
Forced accept.

Computer101
EE Admin