Stringgrid vs Delphi ValueListEditor

Posted by Johan P14205 on March 01, 2005

My Delphi version doesn't have a ValueListEditor component. I think that I can simulate with a Delphi stringgrid. Can you give me some ideas on how to start?

Reply by Guido, DelpiLand Team on March 01, 2005

Yes, you can use a Delphi TStringGrid component for data entry, just like a ValueListEditor (in fact, it's derived from a stringgrid).

With a StringGrid, you can:
- access the individual cells easily:
  S := StringGrid1.Cells[2,1];
puts the text of the third column / second row in variable S (numbering starts at 0);
- change its number of columns and rows, even at runtime under program control:
  StringGrid1.ColCount := 2;
  StringGrid1.RowCount := 100


Start by putting a stringgrid on the form, and give it the name "SG". In the Object Inspector, set the initial number of columns to 2 and rows to 5. We leave the number of "fixed" rows at 1 (and programmatically put header text in them), and we set FixedCols to 0 (no fixed column).

In the Property Editor, set these Options of the StringGrid:
- goEditing: True (allows you to type in the cells)
- goTabs: True (navigates to the next cell with the TAB key)

Put this code in the OnCreate event handler of the form, so that the columns will show "titles" above them:

SG.Cells[0,0] := 'Column 0';
SG.Cells[1,0] := 'Column 1';

Let's dynamically change the number of rows and show some text in the cells. Put this in the event handler of a button:

  Row, Col: integer
  SG.RowCount := 11;
  for Row := 1 to 10 do 
    for Col := 0 to 1 do 
      SG.Cells[Col, Row] := 
       'Col '   + IntToStr(Col) + 
       ', Row ' + IntToStr(Row);

Any more questions? Just ask :)

[ Delphi Tutorials -- by DelphiLand ]