[Lazarus] Dropdown in stringgrid ?

Jesus Reyes A. jesusrmx at gmail.com
Fri Jun 5 15:45:24 CEST 2015


En Fri, 05 Jun 2015 05:55:51 -0500, Koenraad Lelong  
<lazarus2 at de-brouwerij.be> escribió:

> Op 03-06-15 om 01:19 schreef Jesus Reyes A.:
>>
>> In the grid such combobox is called a picklist.
>>
>> 1. Select the grid
>> 2. In the Object Inspector select the columns property and click the
>> "..." button.
>> 3. Add some custom columns
>> 4. Select the column where you wish to add a pick list
>> 5. In the Object Inspector change the ButtonStyle to cbsPickList
>> 6. In the Object Inspector select the PickList property and click the
>> "..." button.
>> 7. Add all options you want to this list.
>> 8. make sure the grid is editable.
>>
> Thanks, just tried this, but it's not what I need.
>
> Like I said, I want to import a csv-file. Then, using the first row, I  
> use a picklist of fieldnames to identify which column of the csv needs  
> to go to which field in the database.
> Maybe this clarifies it better :
> -------------------------------------------------------
> | Field2 | Field1 |        | Field3 |        |        |      <---  
> picklist (Field1, Field2, Field3, blank)
> -------------------------------------------------------
> |Col0Row1|Col1Row1|Col2Row1|Col3Row1|Col4Row1|Col5Row1|
> |Col0Row2|Col1Row2|Col2Row2|Col3Row2|Col4Row2|Col5Row2|
> etc.
>
> This means column0 should got to Field2 of the database, column1 should  
> got to Field1 of the database and column3 should got to Field3 of the  
> database. The rest of the columns are not needed in the database.
>
> Koenraad.
>

So you want the picklist only in the first row. Then use something like:

// use grid's OnSelectEditor
procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow:  
Integer;
   var Editor: TWinControl);
begin
   if aRow=StringGrid1.fixedRows then begin
     Editor := StringGrid1.EditorByStyle(cbsPickList);
     // fill the picklist with field list
     TPickListCellEditor(Editor).Items.CommaText :=  
'Field1,Field2,Field3,etc';
     // or TPickListCellEditor(Editor).Items.Assign(FieldList)
     // or etc
   end else
     Editor := nil; // readonly the rest of rows (or select another editor)
end;

// use grid's OnPickListSelect
procedure TForm1.StringGrid1PickListSelect(Sender: TObject);
begin
   // make something with picklist itemindex, the current grid column is  
given by StringGrid1.Col
end;

Jesus Reyes A.




More information about the Lazarus mailing list