Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

fields pointing to Listbox, combobox items

Posted on 2009-12-17
28
Medium Priority
?
498 Views
Last Modified: 2012-05-08
Greetings Experts.
First, forgive for my english, hope i'll explain properly what i need.
Seconds, I have i little dilemma.

for example, I have a dbgrid, sometimes with more than 1000 records(rows)
there is a field named 'id_group' containing an id number.
I want set dbgrid so that on row click event combobox's  selected item changes depending on id_group in selected dbgrid row.

for now i have used 2 ways to solve this:
    1:   compare group name with combobox item strings, which is terible especialy if used foreign characters it does call errors or bugs.
    2:  making an array  , setting table id as an index , holding  item index etc .... which is stupid thus when table id will eventualy go out of array max range...

any ideas or suggestions how to accomplish this? i bet there shold be an easier solution.
thank you.
0
Comment
Question by:KarlisB
  • 13
  • 9
  • 4
26 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 26072063
you could associate a TList with each Combo item, and in these TList you have all the index of your rows

let's see for one combo item first :

Var
 IndexForItem0:TList;  // declare this as private section of your form
 
// in your FormCreate
...
IndexForItem0:TList.Create;
UpdateIndex(IndexForItem0, ComboBox.Items[0]); // call an update index procedure
...

// in your FormDestroy
...
IndexForItem0.Free;

procedure UpdateIndex(IndexList:TList; Item:String);
Var
 i:integer;
begin
 IndexList.Clear;
 for i:=0 to myGrid.RowCount-1 do if myGrid.Cells[0,i]=item
 Then IndexList.Add(Pointer(i)); // cast the index as a pointer
end;

then you can read all your index values casting back all list items as integer :
 FirstIndex:=Integer(IndexForItem0.Items[0]);
0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 1800 total points
ID: 26072232
I'd use IndexOfObject to find the Item containing an Object previous added to the item.
I assume that you store values into the combobox reading them in a loop from your table.

So if you use combobox.add(table.fieldbyname('group_name').asstring you could instead add an object too containing the id_group value.

Something like

while not table.eof do
begin
  combobox.addobject(table.fieldbyname('group_name').asstring, Pointer(table.fieldbyname('group_id').asInteger));
table.next;
end;

then, to get the right item in the combobox you could call

combobox.indexofobject(Pointer(table.fieldbyname('group_id').asInteger));
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072261
or, if you are using a dbgrid and not a string grid, then it would be a lot better to use a query but it's going to be a difficult one as you have to calculate the index in the list before actually select only the ones you want
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Expert Comment

by:epasquier
ID: 26072380
yes, I think Ferruccio here as the part of the solution I did not cover, which is the association of the DB ID with the combo box selected item. Then you have to maintain a list of rows to select associated with the ID (or with the combobox itemindex, which might be easier)
Does your DB grid display the DB ID of the group ?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072414
a good thing to know is if your data is going to change very often ?
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072536
Khm, did you read the title properly? or read what i explained?
I am using DBGRID, so, you cant get row number, nor in some circumstances RecNo.
ok, with the Tlist it wold solve some problems... but if in the dbgrid is populated like all record in the table (like 1 000 000 rows in theory,  normally i do limit amount of records returned) wont it "kill computer" :D ? i think it would be waste of computer resources, since there are like only  200 group_id's.

in the past i found a solution to asing a value to  the combobox.items:
 
combobox1.Items.AddObject(pg_query_procesi.Fields[0].AsString,pointer(pg_query_procesi.Fields[1].AsInteger));
 or simple
 combobox1.Items.AddObject('texts', pointer(integer));
 id := combobox1.items.objects[combobox1.itemindex];

is there a way to get item index knowing integer value assigned as an object to the item?
probably i have answered my own question, just loop trough all items and compare assigned integer.
i kinda dont like looping...  ~gosh
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072574
am writing too slow,  didnt sav your next messages. Sorry  

Yes, data changes often, and query does contain group field id, but its not displayed in dbgrid and combobox is generated at the start of app or on any update requiring action
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072582
No, this is good enough :

combobox1.Items.AddObject(pg_query_procesi.Fields[0].AsString,pointer(pg_query_procesi.Fields[1].AsInteger));

then

cbindex:=combobox1.Items.IndexOf(Pointer(DB_ID));

where DB_ID is what is in the Fields[1] of your query

but that's only part of the problem, if I read you right, you are searching for a way to automatically select all rows corresponding to the selected group, are you not ?
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072591
"combobox.indexofobject(Pointer(table.fieldbyname('group_id').asInteger));" - might do the trick
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072599
oups mistake. and as Ferrucio said, it's IndexOfObject

cbindex:=combobox1.Items.IndexOfObject(Pointer(DB_ID));
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072620
"but that's only part of the problem, if I read you right, you are searching for a way to automatically select all rows corresponding to the selected group, are you not ?"
I had a need for that, but didnt found out how to do that yet. main problem might be solved thanks to ya... must check first
0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 200 total points
ID: 26072640
Ouuuuu I'm sleepy today. You want the combobox to be selected when the grid change, not the way around (which is much more complex). Then yes you have your answer.
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072673
don't worry, you ain't the only one :)
The answer was always right under my nose, so why didn't i notice that X)
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072680
do you know you can use some dbCombobox that can maintain display value and index already ?
I have used some from DreamCompany, I'll have to check if the Delphi TDBComboBox have this basic functionality
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26072717
Great points assignment.
Congrates...
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072718
I need to add some extra items to the combobox, thats why i use standard combobox
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072729
Really , thank you for the points, but Ferrucio68 as understood and answered the question before I did.
If you have a way to re-do your solution acceptation, I would rather not rob Ferruccio on this one
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072758
I have called for a moderator to fix this. Sorry Ferruccio, I hope it will be
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26072779
Don't worry about it.
The most important is that the right solution rest for any other reader.
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26072802
Sleepyness causes lot of death's.
Ferruccio68, i hope you didn't mind those 50 points,  thus epasquier indirectly pointed out that sleepyness may cause my problem aswell.
I knew how to assign value to an item by using object ,but didnt noticedthat there is a function to ge  item by the object ...  


0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072847
>> The most important is that the right solution rest for any other reader.
Yeah well, even that is screwed. The selected answers are either faulty (I corrected myself the next post) or not relevant to the question.
The only good and complete one is yours : #26072232 .
Mister moderator, you have my blessing to change this
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26072882
I must repeat, don't worry.
I don't mind the points, after all it's Christmas time ;-)

Glad you've learned something new.
Cheers
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072896
Then, Merry Christmas Ferrucio !
0
 
LVL 3

Author Comment

by:KarlisB
ID: 26078859
Oh, my god.
forgive me Ferruccio68, i just now noticed some big mistake lol
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26079844
Well, In my opinion the answer accepted should not be mine but Ferruccio's as he understood the question sooner and his answer is complete : ID #26072232
He offered me the points as a christmas gift, but nevertheless it's not fair and not proper for other users to look at my incomplete answers where a better is just above.

I suppose KarlisB won't mind if you correct this ?
0
 
LVL 3

Author Closing Comment

by:KarlisB
ID: 31667313
Forgive me, as i was sleepy aswell and made terrible mistakes cause of that.
Hope now  everything is ok.

Happy Christmas! :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question