Solved

Copy from a string to a certain cell on dbgrid

Posted on 2004-10-03
8
230 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 153
Adoquery sql  left join does not work 25 98
Tvertscrollbox like a whatsapp layout delete messages 1 23
Twebbrowser add css to the header 3 27
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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