Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a string grid behaviour

Status
Not open for further replies.

sanjna000

Programmer
Aug 1, 2003
132
GB
Hi Guys,
Well I am trying to load data from a .txt file to my string grid. My loading data function works fine. But when i am trying to delete rows in the string grid , it behaves abnormally. Usually in my string grid, if i am trying to delete a row, for instance i ;ve got altogether 4 rows in my string grid and i am trying to delete the 3rd row by deleteing values entered in the 3rd row columns, then third row will be deleted permenently and the fourth row will come to the position of the third row.But after loading data if i am trying to delete the third row then fourth row also will be dissapeared along with the third row. If i am trying to enter values to the third row only it will make visible my fourth row. ( Note: Please make sure for the time been the string grid can have maximum 4 number of rows apart from the title row. so that this problem occurs whenever the time i am trying to delete the last row). I have done this coding in my string grid onselectcell event. Does any one know how to solve this problem?

Many Thanks
Sanjna..
 
Hi,

Procedure DeleteRow(ARow: Integer);

With myStringGrid Do
Begin
If ( ARow < RowCount -1 ) Then
Begin
For i := ARow To RowCount-2 Do
Rows.Assign(Rows[i+1]);
Cells[1, RowCount-1] := '';
Cells[2, RowCount-1] := '';
RowCount := RowCount - 1;
End;
End;

This is the code i used to delete a row . But i guess i have done something wrong in here. Can u guys help me with that?

Many Thanks
Sanjna...
 
Can you submit the exact code that you use to Delete the row? Use copy and paste.

The code you provided does not compile.

Andrew

 
Hi Andrew,
Thanks for your help!
Well in my string grid onselectcellevent i used to call my delete method after checking the condition as below:

If ( ( StringGrid.Cells[1,StringGrid.Row] = '' ) And
( StringGrid.Cells[2,StringGrid.Row] = '' ) And
( StringGrid.Row <> StringGrid.RowCount-1 ) ) Then
Begin
DeleteRow( StringGrid.Row );

The DeleteRow procedure is the same as above.

Waiting for u r reply
Sanjna...
 
Your DeleteRow procedure fails to compile because you need an index after the first Rows in your code
Code:
Rows.Assign(Rows[i+1]);
I would really like to see the actual code that you have tried!!!

I think the following code does what you want although I am a bit confused as to what problem you are experiencing. I've implmenented it as an OnClick event of a button but you could put it into a CellSelect handler if you want. The actual deletion code is in the DeleteRow procedure which has been defined as a public procedure for the form.
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteRow ( myStringGrid.row );
end;

procedure TForm1.DeleteRow(arow: integer);
var
  i: integer;
begin
  with myStringGrid do begin
    if aRow < RowCount - 1 then
      for i := aRow to RowCount - 1 do
        Rows[i].Assign ( Rows[i+1] );
    RowCount := RowCount - 1;
  end;
end;

It would be better design if you passed the grid as a parameter to the DeleteRow procedure instead of the row number. Then the procedure could be used for any grid and not just for myStringGrid.

Andrew
 
Hi Andrew,

Sorry about that. well I have done the mistake. I do have in my code as
Rows.Assign ( Rows[i+1] )

No wonder it gave u a compile error cause when i was typing it i have forgotten to include Rows in to the code which i showed you. anyway the problem is when i am trying to delete the last row (eg. 4th row) it will b deleted and if i am trying to delete the 3rd row ( which is the one above the last row ) then it will delete the 3rd row contents (which is fine) and also will display the contents of the last row which i have already deleted.

do you have ay idea about this? I really need your help Andrew!!!!!!!!!!

Many Thanks,
Sanjna..
 
Try this. I have amended the code to use the StringGrid as the parameter and changed the code to ensure that a blank row is left at the end which is what I think you are requiring.

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteRow ( myStringGrid );
end;

procedure TForm1.DeleteRow(grid: TStringGrid);
var
  i: integer;
begin
  with grid do begin
    if ( Row < RowCount ) and ( Row > 0 ) then begin
      for i := Row to RowCount - 1 do
        Rows[i].Assign ( Rows[i+1] );
      RowCount := RowCount - 1;
      if RowCount = 1 then begin
        RowCount := 2;
        FixedRows := 1;
      end
    end;
  end;
end;

Andrew
 
Hi Andrew,

Still it occurs the same problem i experianced before. I can't figure out what is really wrong with the code. Why when i delete the 3rd row, it displays the contents of the already deleted last row??

Anyway thank you so much for your help!

Sanjna...
 
Well I can't figure out what's wrong with your code either unless you actually copy and paste your code so that I can look at it. It would also be useful to have a copy and paste of the listing of myStringGrid. Here's what my test stringgrid looks like:
Code:
  object myStringGrid: TStringGrid
    Left = 0
    Top = 48
    Width = 441
    Height = 273
    RowCount = 10
    TabOrder = 1
  end
If you want to get good answers out of Tek Tips it is important to ask questions in the right way. The following is good advice:


Andrew
 
ok, Here's my coding

Procedure myStringGrid(Sender: TObject; ACol,
ARow: Longint; Var CanSelect: Boolean);
Var
i: Integer;
Begin
If ( ( myStringGrid.Cells[1,myStringGrid.Row] = '' ) And
( myStringGrid.Cells[2,myStringGrid.Row] = '' ) And
( myStringGrid.Row <> myStringGrid.RowCount-1 ) )Then
Begin
DeleteRow( myStringGrid.Row );
For i := myStringGrid.Row To myStringGrid.RowCount-1 Do
myStringGrid.Cells[0, i] := IntToStr( i );
CanSelect := False;
End
Else If ( (myStringGrid.RowCount-FilledRowCount) = 1 ) And
(myStringGrid.RowCount-1 < MaxPoints) Then
Begin
myStringGrid.RowCount := myStringGrid.RowCount + 1;
myStringGrid.Cells[0, myStringGrid.RowCount-1] := IntToStr( myStringGrid.RowCount-1 );
CanSelect := True;
End
Else
CanSelect := RowComplete( ARow );
End;

Function FilledRowCount: Integer;
Var
rowCounter: Integer;
Begin
Result := 0;
For rowCounter := 1 To myStringGrid.RowCount-1 Do
Begin
If ( ( myStringGrid.Cells[ 1, rowCounter ] <> '' ) Or
( myStringGrid.Cells[ 2, rowCounter ] <> '' ) )Then
Inc( Result );
End;
End;

Procedure DeleteRow(ARow: Integer);
Var i: Integer;
Begin
With myStringGrid Do
Begin
If ( ARow < RowCount -1 ) Then
Begin
For i := ARow to RowCount-2 Do
Rows.Assign(Rows[i+1]);
cells[1, RowCount-1] := '';
cells[2, RowCount-1] := '';
RowCount := RowCount - 1;
End;
End;
End;

Function RowComplete( ARow: Integer ): boolean;
Begin
If ( ARow <> myStringGrid.Row ) And
( ( myStringGrid.Row < myStringGrid.RowCount-1 ) OR
( myStringGrid.Row = MaxPoints ) )Then
Begin
If (myStringGrid.Cells[1,myStringGrid.Row] = '') and (myStringGrid.Cells[2,myStringGrid.Row] = '') then
Result := True
Else If (myStringGrid.Cells[1,myStringGrid.Row]= '') Then
Begin
MessageDlg('Please specify value for ' + myStringGrid.Cells[1,0], mtInformation,
[mbOK], 0 );
myStringGrid.Col := 1;
Result := False;
End
Else If (myStringGrid.Cells[2,myStringGrid.Row] = '') Then
Begin
MessageDlg('Please specify value for ' + myStringGrid.Cells[2,0], mtInformation,
[mbOK], 0 );
myStringGrid.Col := 2;
Result := False;
End
Else
Result := True;
End;

Guess u can get some idea this time.

Many Thanks,
Sanjna...
 
Your first procedure is called MyStringGrid but you also have a StringGrid called MyStringGrid. How did you get that to compile without any errors?

What version of Delphi are you using?

Can you supply the details of the grid called MyStringGrid from the DFM file? Click on the form and then press Alt+F12. This will give you a listing of the form. I just need the description of MyStringGrid.


Andrew
 
Hi Andrew,

I am using Delphi 6.0

Here's the description of myStringGrid:

object myStringGrid: TStringGrid
Left = 0
Top = 0
Width = 243
Height = 469
Align = alLeft
ColCount = 3
DefaultRowHeight = 16
RowCount = 2
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goColSizing, goEditing, goTabs]
TabOrder = 0
OnKeyDown = myStringGridKeyDown
OnKeyPress = myStringGridKeyPress
OnSelectCell = myStringGridSelectCell
ColWidths = (
64
64
64)
 
Your first procedure is called MyStringGrid but you also have a StringGrid called MyStringGrid. How did you get that to compile without any errors?

Andrew
 
ohhh Sorry Andrew,

I don't have a procedure called myStringGrid. It is the myStringGridSelectCell procedure. since i changed my actual name of the string grid to myStringGrid i have by mistake deleted it. Well so sorry about that. but the name of the string grid will remain the same.

Sanju..
 
Hi Andrew,

Finally it did work fine as i expected. I did small changes to the Delete Procedure as below:

Begin
DeletePoint( ARow-1 );
For i:= ARow To myStringGrid.RowCount-1 Do
myStringGrid.Rows.Assign(myStringGrid.Rows[i+1]);
myStringGrid.cells[1, myStringGrid.RowCount-1] := '';
myStringGrid.cells[2, myStringGrid.RowCount-1] := '';
myStringGrid.RowCount := myStringGrid.RowCount - 1;
End;

Thank you so much for all the help u've given to me. All b cos of u r help i was able to do my program really well.
 
Well that code won't compile either.

Why can't you do a simple copy and paste from your IDE into Tek-Tips ?

Select the code you want to copy in the IDE and then copy it to the clipboard using Ctrl-C. Then paste from the clipboard into Tek-Tips using Ctrl-V. It's not hard.


Andrew
 
Well the thing is Andrew i am deleting the point which i have deleted in the string grid from the point collection, ( which i am having in another object ). Since i did not give u my DeletePoint procedure and all that it should not compile with you . By the way it is working fine now. Anyway if u think that u could help me with that more i can copy the code for you. but i don't know whether u will get more and more confused after that...

Thanks for your help!
Sanju...
 
hi sanjna000

On a side, could you enclose any code snippets in
Code:
so you don't get the problem with the italic - then makes it easier for people to read.

lou

 
..sorry, meant to include the
Code:
but didn't uncheck the 'process TGML' checkbox.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top