Henry Olive 5 Posted November 19, 2020 I wish everyone a healthy day. I have a DBGrid and one of a column has Pick List I fill the pick list like first proc. and read the picklist values in second proc. procedure TForm1.DBGrid2ColEnter(Sender: TObject); if DBGrid2.SelectedField <> CDS1.FieldByName('WSNAME') then Exit; while not MyQry.Eof do begin DBGrid2.Columns[ColIndex].PickList.AddObject(MyQry.Fields[1].AsString, Pointer(MyQry.Fields[0].AsString)); MyQry.Next; end; procedure TForm1.DBGrid2ColExit(Sender: TObject); var Inx:Integer; begin if DBGrid2.SelectedField <>CDS1.FieldByName('WSNAME') then Exit; Inx:=DBGrid2.Columns[2].PickList.IndexOf('WSNAME'); //problem is here i get always -1 here CDS1.FieldByName('WSNO').AsString:=IntToStr(Inx); end; what is wrong ? Thank You Share this post Link to post
FPiette 383 Posted November 19, 2020 What is the problem? What error do you get? Which behavior? Share this post Link to post
Lajos Juhász 293 Posted November 19, 2020 There are multiple problems in your code: 1.) It's not a good idea to add the address value of a return value of a function - Pointer(MyQry.Fields[0].AsString) 2.) .PickList.IndexOf('WSNAME') you are searching the index for string 'WSNAME' while you most probably meant to search the index for the value of the selected field: DBGrid2.Columns[2].PickList.IndexOf(DBGrid2.SelectedField.asString) Share this post Link to post
Henry Olive 5 Posted November 19, 2020 Thank you so much FPiette , Lajos Sorry for my poor english I'm adding WSNO (String) and WSNAME (String) fields values in to a picklist (by a query) with below code DBGrid2.Columns[ColIndex].PickList.AddObject(MyQry.Fields[1].AsString,Pointer(MyQry.Fields[0].AsString)); so, in the pick list just WSNAME field values are visible ( MyQry.Fields[1] ) When user choose a WSNAME in the pick list, i need to get WSNO and WSNAME values (choosen by user) ; P.S = If i was dealed same thing with a combobox then i would write below code WSNO:=IntToStr(Integer(Combobox1.Items.Objects[Combobox1.ItemIndex])); Thank You Share this post Link to post
Lajos Juhász 293 Posted November 19, 2020 You are not adding WSNO as I wrote earlier you're adding a pointer to an address of a string returned by a function. You cannot do that. You should try something like this: procedure TForm1.DBGrid2ColEnter(Sender: TObject); if DBGrid2.SelectedField <> CDS1.FieldByName('WSNAME') then Exit; while not MyQry.Eof do begin DBGrid2.Columns[ColIndex].PickList.AddObject(MyQry.Fields[1].AsString, Pointer(MyQry.RecNo)); MyQry.Next; end; procedure TForm1.DBGrid2ColExit(Sender: TObject); var Inx:Integer; begin if DBGrid2.SelectedField <>CDS1.FieldByName('WSNAME') then Exit; Inx:= DBGrid2.Columns[ColIndex].PickList.IndexOf(DBGrid2.SelectedField.asString); MyQry.RecNo:=integer(DBGrid2.Columns[ColIndex].Picklist.Objects[Inx]); CDS1.FieldByName('WSNO').AsString:=MyQry.Fields[0].AsString; end; Share this post Link to post
Guest Posted November 19, 2020 (edited) Cloning data on FDMemTable1 to FDMemTable2, then, it's not necessary re-arrange the cursor on FD1 and so, showing the values on PickList in DBGrid1. Of course, you can to do of another way using CloneCursor or any another technic. var Form1: TForm1; implementation {$R *.dfm} // { Summary: 1 - FDMemTable for save data 1 - FDMemTable for use as clone! } procedure TForm1.btnOpenTablesClick(Sender: TObject); begin FDMemTable1.Active := false; FDMemTable2.Active := false; // FDMemTable1.ResourceOptions.PersistentFileName := 'myData.xml'; FDMemTable1.ResourceOptions.Persistent := true; FDMemTable1.Active := true; // // FDMemTable2.ResourceOptions.Persistent := false; // just for persist my data on "myData.xml" file - not necessary in real world! // FDMemTable2.AttachTable(FDMemTable1.Table, nil); // the magic is here! // // DBGrid2 - really it's not necessary! used only my visual test! // // DBGrid2.Columns.Assign(DBGrid1.Columns); // defining same columns on DBGrid2 based on DBGrid1 // // DataSource2.AutoEdit := false; // just for avoid any user intervension in my test! not necessary // FDMemTable2.ReadOnly := true; // FDMemTable2.Open; end; procedure TForm1.DBGrid1ColEnter(Sender: TObject); var lTColumn: TColumn; begin lTColumn := (Sender as TDBGrid).Columns.Items[(Sender as TDBGrid).SelectedIndex]; // if (lTColumn.Index = 3) { AnotherNames } then begin FDMemTable2.First; while not FDMemTable2.Eof do begin lTColumn.PickList.Add(FDMemTable2.FieldByName('Names').AsString); FDMemTable2.Next; end; end; end; hug Edited November 19, 2020 by Guest Share this post Link to post
Henry Olive 5 Posted November 20, 2020 Thank you so much Lajos, EMailx45 Share this post Link to post