Solved

Copy from a string to a certain cell on dbgrid

Posted on 2004-10-03
8
229 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 171
LAN or WAN ? 11 92
Unique identifier on a terminal server (rdp) 4 55
Tidtcpserver listening on multiports? 1 27
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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