Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy from a string to a certain cell on dbgrid

Posted on 2004-10-03
8
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Accepted Solution

by:
esoftbg earned 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

704 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