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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare values in a String Grid

Status
Not open for further replies.

sanjna000

Programmer
Aug 1, 2003
132
GB
Hi,

Does anyone know how to compare 2 values which is in the same column in a string grid.

I want to compare 2 values in order to sort the values. I wrote my code as follows:

Begin
MYStringGrid := False;
If MYStringGrid.RowCount > 1 Then
While NOT SortedX Do
Begin
SortedX := True;
For i:= 2 To MaxPoints Do
Begin

If ( StrToFloat(MYStringGrid.Cells[1,i]) < StrToFloat(MYStringGrid.Cells[1,i-1]) ) Then
Begin
SortedX := False;
Temp1 := MYStringGrid.Cells[1,i];
Temp2 := MYStringGrid.Cells[2,i];
MYStringGrid.Cells[1,i] := MYStringGrid.Cells[1,i-1];
MYStringGrid.Cells[2,i] := MYStringGrid.Cells[2,i-1];
MYStringGrid.Cells[1,i-1] := Temp1;
MYStringGrid.Cells[2,i-1] := Temp2;
End;

In the OnSelectCell event i have called the sortX function (Above). but my problem is each and everytime i run the program it allows me to enter only one value to the grid. After that it will display an error message saying that 'Error converting floating point numbers' . I know this causes the conversion i have made in my sortX Function. Does anyone know how to fix my problem???????????

Many Thanks
Sanjna....
 
What do you want to happen when you compare a blank cell and a cell containing a number? This is going to occur as soon as you select your second cell to enter a number.

Surely the compiler will show an error when it tries to compile the statement:
Code:
  MYStringGrid := False


Andrew
 
Andrew,

I have made the mistake by writing the code as &quot;MYStringGrid := False&quot; in this forum. What i actually wrote in my program was &quot; SortedX := False &quot;. But i got the compiler error as stated before when comparing the values. My problem is why it doesn't allow me to enter values after entering first value to the grid?

Many Thanks for u r advice
Sanjna...
 
What was the compiler error you got when you compared the two values?

I would have thought that you would get a run time error.

What I think is happening is that you key in a value in one of the cells and then you select another cell but before you can begin to key in the number an OnSelectCell event occurs. In the OnSelectCell event handler the code tries to convert an empty string into a float which will cause an exception.

I suggest that you use the TryStrToFloat function. Your code could look something like:-
Code:
var
  float1, float2: Double;  // or whatever
begin
  ...
  if not TryStrToFloat(MYStringGrid.Cells[1,i],float1) then 
    float1 := 0;
  if not TryStrToFloat(MYStringGrid.Cells[1,i-1],float2) then
    float2 := 0;
  if float1 < float2 then
    ...
But I don't know how you want to handle blank cells. I asked you but you didn't say.

Andrew
 
hi

Just a quickie, TMSSoftware's TAdvStringGrid incorporates sorting of columns of any type, int, str, Datetime etc.

If you have the finances (and it's not expensive), it'll be worth taking a look at I highly recommend it.

lou
 
Hi Andrew & lou,

Thank you so much for both of u r replies. I am trying to solve the problem...........

Thanks Again........
Sanjna...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top