Solved

Copy from a string to a certain cell on dbgrid

Posted on 2004-10-03
8
223 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

14 Experts available now in Live!

Get 1:1 Help Now