Solved

Copy from a string to a certain cell on dbgrid

Posted on 2004-10-03
8
231 Views
Last Modified: 2010-10-05
I have a string (String1:=Edit1.Text) and i want to copy it to a certain cell inside a dbgrid.
I need the code for this.

Thanks in advance.  


0
Comment
Question by:CodedK
  • 5
  • 2
8 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12212652
procedure TForm1.CopyFromEditToDBGrid(Col, Row: Integer; S: string);
var
  FN:     string;
begin
  with DBGrid.DataSource.DataSet do
  begin
    FN := Fields[Col].FullName;
    First;
    MoveBy(Row-1);
    Edit;
    FieldByName(FN).AsString := S;
    Post;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyFromEditToDBGrid(1, 3, Edit1.Text);
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12212709
excuse me:
....
    FN := Fields[Col-1].FullName;
....
0
 
LVL 16

Author Comment

by:CodedK
ID: 12213060
..........
var STR1 : String
Begin

Str1:=Copy(Edit1.Text,1,1);

with DBGrid1.DataSource.DataSet do
  begin
    Str1 := DBGrid1.Fields[col].NUMBER;
    First;
    MoveBy(Row-1);
    Edit;
    FieldByName(Str1).AsString := S;
    Post;
  end;

End

This is the code after few changes but gives many errors.... :(
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
esoftbg earned 75 total points
ID: 12213265
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21154122.zip

you will see there is no errors ....
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12213461
Why not update the value in the dataset and then simply refresh the DBgrid?
You can do the update either with edit-post or with query (update table set yourfield=newstring where ...)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12213469
it could be more simply:

procedure TForm1.CopyFromEditToDBGrid(Col, Row: Integer; S: string);
begin
  with DBGrid.DataSource.DataSet do
  begin
    if (RecordCount>=Row-1) then
    if (FieldCount>=Col-1) then
    begin
      First;
      MoveBy(Row-1);
      Edit;
      Fields[Col-1].AsString := S;
      Post;
    end;
  end;
end;
0
 
LVL 16

Author Comment

by:CodedK
ID: 12219993
Thank you very much.
The code works fine.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12220112
CodedK, you are welcome !

I will post here the code of the accepted example how it is on the page (ofcourse I can not post here the example database which is on the page too) ....
The code below is not a new answer:

unit Unit_Q_21154122;

interface

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

type
  TForm1 = class(TForm)
      qryNAMES: TQuery;
      dsqNAMES: TDataSource;
      DBGrid: TDBGrid;
      DBNavigator1: TDBNavigator;
      Button1: TButton;
      Edit1: TEdit;
      Button2: TButton;
      procedure FormCreate(Sender: TObject);
      procedure Button1Click(Sender: TObject);
      procedure Button2Click(Sender: TObject);
    private   { Private declarations }
    public    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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

procedure PostToDBGridCell(DBGrid: TDBGrid; Col, Row: Integer; S: 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;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostToDBGridCell(DBGrid, 2, 3, Edit1.Text);      // Second Column, third Row
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  PostToDBGridCell(DBGrid, 2, 3, Button2.Caption); // Second Column, third Row
end;

end.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 118
Delphi XE10 Round Image 2 183
Delphi and Access based Enumeration 9 77
Tvertscrollbox like a whatsapp layout 5 50
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 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