Solved

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

Posted on 2004-10-05
17
829 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:CodedK
17 Comments
 
LVL 16

Author Comment

by:CodedK
ID: 12226177
I use descenting filter on the 1st column...
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12226248
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12226678
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 ;-)
0
 
LVL 16

Author Comment

by:CodedK
ID: 12227824
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
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12228582
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;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12228929
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;
0
 
LVL 16

Author Comment

by:CodedK
ID: 12236848
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.. ?????

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12236958
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)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Expert Comment

by:esoftbg
ID: 12237056
         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 ?
0
 
LVL 16

Author Comment

by:CodedK
ID: 12240192
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.
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 100 total points
ID: 12240409
unit Unit1_Q_21156333;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, Buttons, StdCtrls, ExtCtrls, DBCtrls;

type
  TCellRec = record
    Col:     Integer;
    Row:     Integer;
    I:       Integer;
  end;
  TCellArr = array of TCellRec;
  TForm1 = class(TForm)
      qryNAMES: TQuery;
      dsqNAMES: TDataSource;
      DBGrid: TDBGrid;
      DBNavigator1: TDBNavigator;
    btnCopyCellToCell: TButton;
      procedure FormCreate(Sender: TObject);
      procedure btnCopyCellToCellClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private   { Private declarations }
      CellArr:  TCellArr;
    public    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  qryNAMES.Active := True;
end;

procedure CopyDBGridCellToCell(DBGrid: TDBGrid; ColSource, RowSource, ColTarget, RowTarget: Integer; T: string; CellArr:  TCellArr);
var
  I:      Integer;
  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;
          S := '';
          for I := 0 to Length(CellArr)-1 do
          begin
            S := S + qry.Fields[ColSource-1].AsString[CellArr[I].I];
            qry.Next;
          end;
          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
  SetLength(CellArr, 4);

  CellArr[0].Col := 2;
  CellArr[0].Row := 1;
  CellArr[0].I := 1;

  CellArr[1].Col := 2;
  CellArr[1].Row := 2;
  CellArr[1].I := 2;

  CellArr[2].Col := 2;
  CellArr[2].Row := 3;
  CellArr[2].I := 3;

  CellArr[3].Col := 2;
  CellArr[3].Row := 4;
  CellArr[3].I := 4;

  T := qryNames.SQL.Text;
  CopyDBGridCellToCell(DBGrid, 2, 3, 2, 8, T, CellArr);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetLength(CellArr, 0);
end;

end.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12240477
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;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12240504
Take a look at the Length(Str5) ....
It may be biggest than the size of the field into that will be copied;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12240619
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21156333.zip
0
 
LVL 16

Author Comment

by:CodedK
ID: 12241021
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 :)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12241092
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 ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12241103
I am glad that you found and solve the problem !
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now