Jump to content
Henry Olive

Getting Value from Pick List

Recommended Posts

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

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

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

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

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.

image.thumb.png.40fc09e799e048bc92d6cf87abc2bd24.png

 

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 by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×