Link to home
Start Free TrialLog in
Avatar of KarlisB
KarlisBFlag for Latvia

asked on

fields pointing to Listbox, combobox items

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.
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

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]);
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy 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
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
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 ?
a good thing to know is if your data is going to change very often ?
Avatar of KarlisB

ASKER

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
Avatar of KarlisB

ASKER

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
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 ?
Avatar of KarlisB

ASKER

"combobox.indexofobject(Pointer(table.fieldbyname('group_id').asInteger));" - might do the trick
oups mistake. and as Ferrucio said, it's IndexOfObject

cbindex:=combobox1.Items.IndexOfObject(Pointer(DB_ID));
Avatar of KarlisB

ASKER

"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
SOLUTION
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 KarlisB

ASKER

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)
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
Great points assignment.
Congrates...
Avatar of KarlisB

ASKER

I need to add some extra items to the combobox, thats why i use standard combobox
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
I have called for a moderator to fix this. Sorry Ferruccio, I hope it will be
Don't worry about it.
The most important is that the right solution rest for any other reader.
Avatar of KarlisB

ASKER

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 ...  


>> 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
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
Then, Merry Christmas Ferrucio !
Avatar of KarlisB

ASKER

Oh, my god.
forgive me Ferruccio68, i just now noticed some big mistake lol
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 ?
Avatar of KarlisB

ASKER

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

Happy Christmas! :)