CodedK
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.FieldD efs.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.Fie lds[0].AsS tring; //Try to copy from cell 1st Column /1st Row
Str2:=DataSource1.DataSet. Fields.Fie lds[1].AsS tring; //Try to copy from cell 1st Column /2nd Row
Str3:=DataSource1.DataSet. Fields.Fie lds[2].AsS tring;
Str4:=DataSource1.DataSet. Fields.Fie lds[3].AsS tring;
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
.......
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.FieldD
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
begin
//--- Create Maximum
DataSource1.DataSet.First;
Str1:=DataSource1.DataSet.
Str2:=DataSource1.DataSet.
Str3:=DataSource1.DataSet.
Str4:=DataSource1.DataSet.
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
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.
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 ;-)
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 ;-)
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.Filter ed:=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
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.Filter
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(DBGri d: 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].As String;
DBGrid.DataSource.DataSet. First;
DBGrid.DataSource.DataSet. MoveBy(Row Target-1);
DBGrid.DataSource.DataSet. Edit;
DBGrid.DataSource.DataSet. Fields[Col Target-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(DBGri d, 2, 3, 2, 8);
end;
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].As
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
finally
qry.Active := False;
qry.Destroy;
end;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender
begin
CopyDBGridCellToCell(DBGri
end;
a little bit improved version:
procedure CopyDBGridCellToCell(DBGri d: 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].As String;
DBGrid.DataSource.DataSet. First;
DBGrid.DataSource.DataSet. MoveBy(Row Target-1);
DBGrid.DataSource.DataSet. Edit;
DBGrid.DataSource.DataSet. Fields[Col Target-1]. AsString := S;
DBGrid.DataSource.DataSet. Post;
finally
qry.Active := False;
qry.Destroy;
end;
end;
end;
end;
end;
procedure TForm1.btnCopyCellToCellCl ick(Sender : TObject);
var
T: string;
begin
T := qryNames.SQL.Text;
CopyDBGridCellToCell(DBGri d, 2, 3, 2, 8, T);
end;
procedure CopyDBGridCellToCell(DBGri
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].As
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
finally
qry.Active := False;
qry.Destroy;
end;
end;
end;
end;
end;
procedure TForm1.btnCopyCellToCellCl
var
T: string;
begin
T := qryNames.SQL.Text;
CopyDBGridCellToCell(DBGri
end;
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.. ?????
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.Fie lds[0].AsS tring; //Try to copy from cell 1st Column /1st Row
Str2:=DataSource1.DataSet. Fields.Fie lds[1].AsS tring; //Try to copy from cell 1st Column /2nd Row
Str3:=DataSource1.DataSet. Fields.Fie lds[2].AsS tring;
Str4:=DataSource1.DataSet. Fields.Fie lds[3].AsS tring;
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)
> Maybe the problem is when i try to copy to Str(x) from the first four rows of column 1
Str1:=DataSource1.DataSet.
Str2:=DataSource1.DataSet.
Str3:=DataSource1.DataSet.
Str4:=DataSource1.DataSet.
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(Row Target-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 ?
DBGrid.DataSource.DataSet.
Obviously I did not understand your question and made a wrong example .... Do you need a correct one ?
ASKER
About your last comment Esoftbg...
DBGrid.DataSource.DataSet. First; // Sets DBGrid "First" Row to be current
DBGrid.DataSource.DataSet. MoveBy(Row Target-1); // Moves DBGrid "RowTarget" Row to be current...
I tried this :
procedure TForm1.Button3Click(Sender : TObject);
begin
DBGrid1.DataSource.DataSet .First;
Str1:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [0].AsStri ng;
//DBGrid1.DataSource1.Data Set.First;
DBGrid1.DataSource.DataSet .MoveBy(-1 ); // I also tried MoveBy(1)
Str2:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [1].AsStri ng;
//DBGrid1.DataSource1.Data Set.First;
DBGrid1.DataSource.DataSet .MoveBy(-2 );
Str3:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [2].AsStri ng;
//DBGrid1.DataSource1.Data Set.First;
DBGrid1.DataSource.DataSet .MoveBy(-3 );
Str4:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [3].AsStri ng;
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.Fi elds[somet hing].AsIn teger;
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.
DBGrid.DataSource.DataSet.
DBGrid.DataSource.DataSet.
I tried this :
procedure TForm1.Button3Click(Sender
begin
DBGrid1.DataSource.DataSet
Str1:=DBGrid1.DataSource.D
//DBGrid1.DataSource1.Data
DBGrid1.DataSource.DataSet
Str2:=DBGrid1.DataSource.D
//DBGrid1.DataSource1.Data
DBGrid1.DataSource.DataSet
Str3:=DBGrid1.DataSource.D
//DBGrid1.DataSource1.Data
DBGrid1.DataSource.DataSet
Str4:=DBGrid1.DataSource.D
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
begin
DBGrid1.DataSource.DataSet .First;
Str1:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [0].AsStri ng;
DBGrid1.DataSource.DataSet .Next; // Next record is the Second one;
Str2:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [1].AsStri ng;
DBGrid1.DataSource.DataSet .Next; // Next record is the Third one;
Str3:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [2].AsStri ng;
DBGrid1.DataSource.DataSet .Next; // Next record is the Fourth one;
Str4:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [3].AsStri ng;
Str5:=Str1+Str2+Str3+Str4;
ShowMessage(Str5); // A random cell value appears...
PostToDBGridCell(DBGrid1, 2, 1,Str5);
end;
DBGrid1.DataSource.DataSet
Str1:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str2:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str3:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str4:=DBGrid1.DataSource.D
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;
It may be biggest than the size of the field into that will be copied;
ASKER
Thanks Esoftbg.
Found the problem.. :)
>> DBGrid1.DataSource.DataSet .Next; // Next record is the Third one;
Str3:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [2].AsStri ng;
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.D ataSet.Fie lds.Fields [0].AsStri ng;
DBGrid1.DataSource.DataSet .Next;
Str2:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [0].AsStri ng;
DBGrid1.DataSource.DataSet .Next;
Str3:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [0].AsStri ng;
DBGrid1.DataSource.DataSet .Next;
Str4:=DBGrid1.DataSource.D ataSet.Fie lds.Fields [0].AsStri ng;
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 :)
Found the problem.. :)
>> DBGrid1.DataSource.DataSet
Str3:=DBGrid1.DataSource.D
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
Str1:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str2:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str3:=DBGrid1.DataSource.D
DBGrid1.DataSource.DataSet
Str4:=DBGrid1.DataSource.D
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 !
ASKER