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.
LVL 3
KarlisBAsked:
Who is Participating?
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
a good thing to know is if your data is going to change very often ?
0
 
KarlisBAuthor Commented:
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
 
KarlisBAuthor Commented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
KarlisBAuthor Commented:
"combobox.indexofobject(Pointer(table.fieldbyname('group_id').asInteger));" - might do the trick
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
oups mistake. and as Ferrucio said, it's IndexOfObject

cbindex:=combobox1.Items.IndexOfObject(Pointer(DB_ID));
0
 
KarlisBAuthor Commented:
"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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
KarlisBAuthor Commented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Great points assignment.
Congrates...
0
 
KarlisBAuthor Commented:
I need to add some extra items to the combobox, thats why i use standard combobox
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
I have called for a moderator to fix this. Sorry Ferruccio, I hope it will be
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Don't worry about it.
The most important is that the right solution rest for any other reader.
0
 
KarlisBAuthor Commented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
>> 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
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
Then, Merry Christmas Ferrucio !
0
 
KarlisBAuthor Commented:
Oh, my god.
forgive me Ferruccio68, i just now noticed some big mistake lol
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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
 
KarlisBAuthor Commented:
Forgive me, as i was sleepy aswell and made terrible mistakes cause of that.
Hope now  everything is ok.

Happy Christmas! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.