Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Dbgrid Copy a cell to another cell - Problem.. Error

This is a piece of code
.......
If Key=13 then
Begin

Str1:=Copy(Edit1.Text,1,1);
Str2:=Copy(Edit1.Text,2,1);
Str3:=Copy(Edit1.Text,3,1);
Str4:=Copy(Edit1.Text,4,1);
Edit1.Visible:=False;
Dbgrid1.Visible:=True;

DataSource1.DataSet.FieldDefs.Clear;

PostToDBGridCell(DBGrid1, 1, 1,Str1);  ///// Column ,Row , String
PostToDBGridCell(DBGrid1, 1, 2,Str2);
PostToDBGridCell(DBGrid1, 1, 3,Str3);
PostToDBGridCell(DBGrid1, 1, 4,Str4);

DBGrid1.Refresh;
End;
....
....
procedure TForm1.Button3Click(Sender: TObject);
begin

//--- Create Maximum
DataSource1.DataSet.First;

Str1:=DataSource1.DataSet.Fields.Fields[0].AsString;  //Try to copy from cell  1st Column /1st Row
Str2:=DataSource1.DataSet.Fields.Fields[1].AsString; //Try to copy from cell  1st Column /2nd Row
Str3:=DataSource1.DataSet.Fields.Fields[2].AsString;
Str4:=DataSource1.DataSet.Fields.Fields[3].AsString;



Str5:=Str1+Str2+Str3+Str4;
PostToDBGridCell(DBGrid1, 2, 1,Str5);       //////  2 Column 1 Row
end;
................

The problem is that when i use PostToDBGridCell :

PostToDBGridCell(DBGrid1, 2, 1,Str5);       //////  2 Column 1 Row


PostToDBGridCell is a procedure that works fine when i try to copy text from Edit1.Text
to the cells in the 1 column.

Problem :
Only one character appear in the 1st cell, 1st Row of Column 2 (Character from  Str4).


Maybe the problem is when i try to copy to Str(x) from the first four rows of column 1
I dont know.
The application is higly unstable... Sometimes reboot required...
I hope i was clear enough :/

Thanks in advance
Avatar of CodedK
CodedK
Flag of Greece image

ASKER

I use descenting filter on the 1st column...
Did you try to ShowMessage() with your resulting string (after the sum)?
HowAbout your Procedure postToDBGrid... does it allow a string ? Paste the procedure also if you want your problem solved. It maybe coming from the procedure...the error.
not read the entire question complete

just to say forget cells and rows use fields and datarecord,
means you cannot address a "dbgrid-cell" with row and column

looking later again

meikl ;-)
Avatar of CodedK

ASKER

How can i do that meikl?
I tried to do that with fields ..but not datarecords...
Dont have enough expirience with Database and all that...

But the problem is why PostToDBGridCell works for one cell and the next moment dont work for the other cell (Next Column..) ???

The procedure is :

procedure PostToDBGridCell(DBGrid: TDBGrid; Col, Row: Integer; S: string);
(*var FN:string;*)
begin
  with DBGrid.DataSource.DataSet do
  begin
    if (State<>dsBrowse) then
      Cancel;
    if (RecordCount>=Row) then
    begin
      if (FieldCount>=Col) then
      begin
        First;
        MoveBy(Row-1);
        Edit;
        Fields[Col-1].AsString := S;
        Post;
      end;
    end;
  end;
end;


Is there a way to temporary disable the filter (Descenting) of a Column and then filter it back ???

I think that this may cause the problems.

I tried with DataSource1.DataSet.Filtered:=False;  
but gives a read-only error for table1 and then when i set the table1 to read-only=false
i get a memory error.......

Thanks
procedure CopyDBGridCellToCell(DBGrid: TDBGrid; ColSource, RowSource, ColTarget, RowTarget: Integer);
var
  qry:    TQuery;
  S:      string;
begin
  with DBGrid.DataSource.DataSet do
  begin
    if (State<>dsBrowse) then
      Cancel;
    if ((RecordCount>=RowSource) and (RecordCount>=RowTarget)) then
    begin
      if ((FieldCount>=ColSource) and (FieldCount>=ColTarget)) then
      begin
        qry := TQuery.Create(nil);
        try
          qry.SQL.Text := 'SELECT * FROM SINGERS';
          qry.Active := True;
          qry.MoveBy(RowSource-1);
          S := qry.Fields[ColSource-1].AsString;
          DBGrid.DataSource.DataSet.First;
          DBGrid.DataSource.DataSet.MoveBy(RowTarget-1);
          DBGrid.DataSource.DataSet.Edit;
          DBGrid.DataSource.DataSet.Fields[ColTarget-1].AsString := S;
          DBGrid.DataSource.DataSet.Post;
        finally
          qry.Active := False;
          qry.Destroy;
        end;
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyDBGridCellToCell(DBGrid, 2, 3, 2, 8);
end;
a little bit improved version:

procedure CopyDBGridCellToCell(DBGrid: TDBGrid; ColSource, RowSource, ColTarget, RowTarget: Integer; T: string);
var
  qry:    TQuery;
  S:      string;
begin
  with DBGrid.DataSource.DataSet do
  begin
    if (State<>dsBrowse) then
      Cancel;
    if ((RecordCount>=RowSource) and (RecordCount>=RowTarget)) then
    begin
      if ((FieldCount>=ColSource) and (FieldCount>=ColTarget)) then
      begin
        qry := TQuery.Create(nil);
        try
          qry.SQL.Text := T;
          qry.Active := True;
          qry.MoveBy(RowSource-1);
          S := qry.Fields[ColSource-1].AsString;
          DBGrid.DataSource.DataSet.First;
          DBGrid.DataSource.DataSet.MoveBy(RowTarget-1);
          DBGrid.DataSource.DataSet.Edit;
          DBGrid.DataSource.DataSet.Fields[ColTarget-1].AsString := S;
          DBGrid.DataSource.DataSet.Post;
        finally
          qry.Active := False;
          qry.Destroy;
        end;
      end;
    end;
  end;
end;

procedure TForm1.btnCopyCellToCellClick(Sender: TObject);
var
  T:      string;
begin
  T := qryNames.SQL.Text;
  CopyDBGridCellToCell(DBGrid, 2, 3, 2, 8, T);
end;
Avatar of CodedK

ASKER

Thank you very much Esoftbg.
I should mention that the previous code i used is also from u. :)

But what is the problem with my code. I use PostToDBGrid for the 1st column and works but when i try to the next doesn't.. ?????

Hi CodedK,

> Maybe the problem is when i try to copy to Str(x) from the first four rows of column 1

Str1:=DataSource1.DataSet.Fields.Fields[0].AsString;  //Try to copy from cell  1st Column /1st Row
Str2:=DataSource1.DataSet.Fields.Fields[1].AsString; //Try to copy from cell  1st Column /2nd Row
Str3:=DataSource1.DataSet.Fields.Fields[2].AsString;
Str4:=DataSource1.DataSet.Fields.Fields[3].AsString;

that is not Str(x) from the first four rows of column 1,
that is Str(x) from the first four columns of the current row of the DBGrid (Only the user knows which row is current)
         DBGrid.DataSource.DataSet.First;                            // Sets DBGrid "First" Row to be current
          DBGrid.DataSource.DataSet.MoveBy(RowTarget-1);  // Moves DBGrid "RowTarget" Row to be current

Obviously I did not understand your question and made a wrong example .... Do you need a correct one ?
Avatar of CodedK

ASKER

About your last comment Esoftbg...

DBGrid.DataSource.DataSet.First;                           // Sets DBGrid "First" Row to be current
DBGrid.DataSource.DataSet.MoveBy(RowTarget-1);  // Moves DBGrid "RowTarget" Row to be current...

I tried this :


procedure TForm1.Button3Click(Sender: TObject);
begin

DBGrid1.DataSource.DataSet.First;
Str1:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;

//DBGrid1.DataSource1.DataSet.First;
DBGrid1.DataSource.DataSet.MoveBy(-1);         // I also tried MoveBy(1)
Str2:=DBGrid1.DataSource.DataSet.Fields.Fields[1].AsString;

//DBGrid1.DataSource1.DataSet.First;
DBGrid1.DataSource.DataSet.MoveBy(-2);
Str3:=DBGrid1.DataSource.DataSet.Fields.Fields[2].AsString;

//DBGrid1.DataSource1.DataSet.First;
DBGrid1.DataSource.DataSet.MoveBy(-3);
Str4:=DBGrid1.DataSource.DataSet.Fields.Fields[3].AsString;

Str5:=Str1+Str2+Str3+Str4;
ShowMessage(Str5); // A random cell value appears...


PostToDBGridCell(DBGrid1, 2, 1,Str5);
end;
-----------------------------------------------------------------------------------------------
The result is the same as before when i had
DBGrid1.DataSource.DataSet.Fields.Fields[something].AsInteger;
Only one value was written in the next column (Random)...???

Things become very unpredictable.

Everytime i run the application different errors come up.
I had to close Delphi and restructure the table many times...
......
If u can write an example code...please :)
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
begin
  DBGrid1.DataSource.DataSet.First;

  Str1:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;

  DBGrid1.DataSource.DataSet.Next;         // Next record is the Second one;
  Str2:=DBGrid1.DataSource.DataSet.Fields.Fields[1].AsString;

  DBGrid1.DataSource.DataSet.Next;         // Next record is the Third one;
  Str3:=DBGrid1.DataSource.DataSet.Fields.Fields[2].AsString;

  DBGrid1.DataSource.DataSet.Next;         // Next record is the Fourth one;
  Str4:=DBGrid1.DataSource.DataSet.Fields.Fields[3].AsString;

  Str5:=Str1+Str2+Str3+Str4;
  ShowMessage(Str5); // A random cell value appears...

  PostToDBGridCell(DBGrid1, 2, 1,Str5);
end;
Take a look at the Length(Str5) ....
It may be biggest than the size of the field into that will be copied;
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21156333.zip
Avatar of CodedK

ASKER

Thanks Esoftbg.

Found the problem.. :)

>>  DBGrid1.DataSource.DataSet.Next;         // Next record is the Third one;
      Str3:=DBGrid1.DataSource.DataSet.Fields.Fields[2].AsString;

But the problem was in Fields[2].. I thought that [2] was 2 down from the first cell moving vertical.
But it was moving horizontal ...
So when i tried Fields[1] then [2] ... with next .. it was moving diagonal...!
--->Error (List out of bounds and memory access violations)

So.. what i was looking for was

DBGrid1.DataSource.DataSet.First;
Str1:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;

DBGrid1.DataSource.DataSet.Next;
Str2:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;


DBGrid1.DataSource.DataSet.Next;
Str3:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;

DBGrid1.DataSource.DataSet.Next;
Str4:=DBGrid1.DataSource.DataSet.Fields.Fields[0].AsString;

Str5:=Str1+Str2+Str3+Str4;
ShowMessage(Str5);

Thank you very much for you time :))
I will grand as accepted answer your procedure ... Cause it automates what i was looking for.
Though i dont understand much...

Question in your procedure you have TQuery and SQL mentioned....
I use a DBase table is there any problem with that?

Thank you again man :)
I use TQuery and SQL to access a Paradox table via BDE, so there is no problem to use TQuery and SQL to access DBase via BDE. If you access DBase via ADO you may use TADOQuery and SQL .... There is no problem ....
I am glad that you found and solve the problem !