View Full Version : Help:TDBGrid Manipulation

01-01-2009, 10:20 PM
Hi can someone please tell me if it is possible (and if it is then how ..) to manipulate rows and cells in the TDBGrid component ?
Untill now I have created a TDBGrid and named 3 Columns in it but I can't find a way to manipulate the rows and cells of the grid ..
(For example how can I assign a value to a cell .. without the use of other components ..)

02-01-2009, 05:14 PM
to manipulate rows and cells in the TDBGrid component ?

Note that you are saying TDBGRID, which it is a component for be used to edit data in tables from a database; this mean this is used for edit records from data stored in disk in form of popular databases, like Dbase, paradox, MSacces, SQLs etc.

If that is wath you want to do then you have to use the datasource property to link tdbgrid with a TDATASOURCE component, and this one have to be linked with a TDATASET component where is defined which datababase and table is going to be opened.

If what you want in your program is just show/edit some data in tabular way then use STRINGGRID component (found in the additional pallete) instead, this one allow you to manipulate string data using rows and colums.

good luck.

02-01-2009, 08:03 PM
Yup StringGrid was the thing I was looking for .. thnx for the help !

03-01-2009, 11:51 PM
Hey is there a way to know if the user has finished writing in a cell ?

05-01-2009, 05:30 PM
Hey is there a way to know if the user has finished writing in a cell ?

Most controls have an "onexit" event which is triggered when the user try to move to another control (so you can assume the user finished doing editing that control"

However that is not usefull for the stringgrid component, becouse you want an event when the user try to move from cell to another cell; unfortunately there is none event exactly like that.

If want you want is to validate what the user entry that there is a kind alternative using "ongeteditmaks" event where you can defined a mask string to validad the characters before it gets entered.

If want you want is to "get" what the user write in a cell after it move to another cell then i think you can do that coding few simple own stuff, like using your own prior_row and prior_columns variables to store which row and column the user was editing, and use "onsetedittext" to inspect which column and row is the user is currently editing if are diffrent then inspect the content of prior row and column cell using your custom variables, do what you want to do and then update your custom variables with current.

good luck.

12-01-2009, 05:15 PM
Well what I wanted to do is that after the user entered a value to every cell in the last row the grid should add a new blank row..
thnx for the help .. :)

15-03-2009, 01:07 AM
Just thought i'd add some code if you're still playing around with this. Put a stringgrid on a form and assign these 2 events onsetedittext and onkeyup. You'll also need a global boolean variable (i called mine newrow) to see if a new row is needed.

procedure TForm1.StringGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
{ if user presses return and new row is allowed then add 1 to row count, set the cursor to the first cell in new row (allowing for fixed columns) }
if (Key=VK_RETURN) and (newrow) then begin

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
k : Integer;
{are we editing the last cell in the row? if not, we don't need to check and newrow is not needed(false) }
with StringGrid1 do begin
if (EditorMode) and (ACol=ColCount-1) and (ARow=RowCount-1) then begin
newrow:=True; //default is new row needed
{loop through columns (allowing for fixed ones)
for k:=FixedCols to ColCount-1 do
if Cells[k,Row]='' then begin
{if any cell is blank then stop the loop and new row isn't needed }
else newrow:=false;