#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0

    stringgrid to dbgrid(adotable)


    Hello
    I'm trying to fill a dbgrid from a stringgrid but I can't get the result
    I hope you can help me
    Thanks
    Dan

    This is my code:

    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var
    r, c : integer;
    begin
    c := 0 - 1;
    for r := 1 to SG1.RowCount - 1 do
    begin
    ADOTable1.Append; 
    ADOTable1.FieldByName(ADOTable1.Fields [r].FieldName).AsString := SG1.Cells[c+1, r+1];// here tagid assigned to tagid field
    ADOTable1.Post;
    end;
    end;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    Do you have a TDatasource where the DataSet property is set to ADOTable1 and is the DBGrid's DataSource property set to that TDatasource?

    Also, why are you setting C to equal -1

    Code:
       c := 0 - 1;
    and then just using C + 1 in
    Code:
      ...:= SG1.Cells[c+1, r+1]
    Why not just..
    Code:
    C := 0;
    Last edited by majlumbo; May 18th, 2015 at 01:20 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Hello
    About your question about c, for one reason if a set c to 0, it will get the second column of the data grid, i don’t know why…
    I think maybe could be an error in the “for”
    If I execute the following code I get the first column - first row value of the datagrid in the second column - first row of the dbgrid (the first column is an auto increment field)
    So with the “for” I can’t fill all the fields
    Thanks for your help



    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var
    r, c : integer;
    begin
    c := 0;
    r := 1;
    //for r := 1 to SG1.RowCount - 1 do
    //begin
    ADOTable1.Append;
    ADOTable1.FieldByName(ADOTable1.Fields [r].FieldName).AsString := SG1.Cells[c, r];
    ADOTable1.Post;
    //end;
    end;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    How many fields does your table have? I'm assuming there's only one. If in fact there's only one then
    Code:
    ADOTable1.Fields [r].FieldName
    can't be correct, since r will range from 1 to SG1.RowCount - 1

    Fields property is a 0 (Zero) based array, so you are never setting the 1st field, since the lowest r can be is 1 (which corresponds to the 2nd field).
    Last edited by majlumbo; May 19th, 2015 at 08:07 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Hello
    The size of the string grid is always variable
    How can I get the content of a stringgrid (n rows – always variable) to a dbgrid
    Could you help me with a basic example, ¿how would you do that?
    I will appreciate it
    Thanks
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Update
    I have been working on the code
    Some fixes but the main issue remain
    I got this (see the image) with the following code

    Image:
    Simple File Sharing and Storage.

    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var
    r, c, f, i : integer;
    begin
    c := 0;
    r := 1;
    f := 0;
    for i := 1 to SG1.ColCount -1 do
    begin
    ADOTable1.Append;
    ADOTable1.Fields[f].Value := SG1.Cells[c, r];
    inc (c);
    inc (r);
    inc (f);
    ADOTable1.Post;
    end;
    end;

    Comments on this post

    • Will-O-The-Wisp agrees : Hello - in the future please put a [code] before your code and a [(backslash)code] after your code. This will preserve formatting and make it easier for our users to read. I've added code tags to your code here :)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    Since I only have the code you provided as a means to figure out what it is you are attempting to do, I have to make some assumptions.
    The reason I asked how many fields your table (database table - not stringgrid), is to figure out how many columns there were to update.
    It looks like to me there is in fact only one (1) column in your database table... Is that correct?

    And it looks like to me that you want to take the value in the 1st column (index 0) of your StringGrid as the value to insert into your table. Is that correct?

    if both of those assumptions are correct, then possibly this will do what you are looking for.
    Code:
    var
       Col: Integer;
       Row: Integer;
    begin
       ADOTable1.DisableControls;//This will stop the application from updating the screen until all the changes have been accomplished.
       try
          Col := 0;
          for Row := 1 to SG1.RowCount - 1 do //this starts on the 2nd row, appropriate if your 1st row is a header if not, change to for Row := 0...
          begin
             if trim(SG1.Cells[Col, Row]) <> '' then //only insert the record where the value isn't blank.
             begin
                ADOTable1.Insert; 
                ADOTable1.Fields[0].AsString := SG1.Cells[Col, Row];
                ADOTable1.Post;
             end;
          end;
       finally
          ADOTable1.EnableControls;
       end;
    end;
    If you do in fact have the TDatasource and the TDBGrid correctly configured as I mentioned in my first reply, you should get all the records showing up in the DBGrid
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Hello
    There are 11 columns and N rows (always variable)
    Yes, I want to take the index 0 of the first column
    Thanks for your help
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    Where are you getting the values for the other 10 columns in your table? You would need to set those values before you call the Post method if you intend to set them in the same routine.
    Code:
    ADOTable1.Fields[0].AsString := SG1.Cells[Col, Row];
    ADOTable1.Fields[1].AsString := 'XXX';
    ...
    ADOTable1.Fields[9].AsString := 'YYY';
    ADOTable1.Post;
    Last edited by majlumbo; May 20th, 2015 at 01:53 PM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Hello
    All the data in the stringgrid is pre-load with another function (import csv to string grid)
    Please look this image
    Code:
     http://www.mediafire.com/view/br13h7s35r3aseq/image.png
    That’s what I got with this code…
    Code:
     
    procedure TForm1.Button2Click(Sender: TObject);
    var
    r, c, f, i : integer;
    begin
    c := 0;
    r := 1;
    f := 0;
    for i := 1 to SG1.ColCount -1 do
    begin
    ADOTable1.Append;
    ADOTable1.Fields[f].Value := SG1.Cells[c, r];
    inc (c);
    inc (r);
    inc (f);
    ADOTable1.Post;
    end;
    end;
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    This is your code
    I made some changes to use it with “for” and for one reason I received the same result
    ¿What I’m doing wrong?



    Code:
    procedure TForm1.btnLoadDbClick(Sender: TObject);
    var
       Col: Integer;
       Row: Integer;
       F : Integer;
    begin
       ADOTable1.DisableControls;//This will stop the application from updating the screen until all the changes have been accomplished.
       try
          Row := 1;
          F := 0;
          for Col := 0 to SG1.ColCount do //this starts on the 2nd row, appropriate if your 1st row is a header if not, change to for Row := 0...
          begin
             if trim(SG1.Cells[Col, Row]) <> '' then //only insert the record where the value isn't blank.
             begin
                ADOTable1.Insert;
                ADOTable1.Fields[F].value := SG1.Cells[Col, Row];
                ADOTable1.Post;
                F := F+1;
             end;
          end;
       finally
          ADOTable1.EnableControls;
       end;
    end;
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    Your problem is that you are appending a new record for each column in your stringgrid. What you want is to append a new record on each new row in your stringgrid.
    Code:
    var
      Col: Integer;
      Row: Integer;
    begin
      ADOTable1.DisableControls;
      try
        for Row := 1 to SG1.RowCount - 1 do //Start at 2nd row - if you want 1st row change to for "Row 0 to SG1.RowCount - 1 do"
        begin
          ADOTable1.Append;//append record for the new row in SG.
          for Col := 0 to SG1.ColCount - 1 do //for each column set the value of the db column
            ADOTable1.Fields[Col].AsString := SG1.Cells[Col, Row];
          ADOTable1.Post; //after all columns are updated, post it.
        end;
      finally
        ADOTable1.EnableControls;
      end;
    end;

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping out here!
    • dantao agrees : Thanks so much!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Thanks so much, it works
    God Bless
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    9
    Rep Power
    0
    Hello
    I just have one more question
    Everything Works while I was working on the first environment (connected to a sql database through – SQL server native client)
    Then i tried to use the same code with a mdb (Access DB) through "Microsoft jet 4 ole DB provider"
    The code just wrote the first row and then gave an error
    ¿The way the data is stored into a mdb files could be different?
    ¿Where the code should change?
    Thanks
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    Sorry, that's just too broad of a question to be able to answer without know more about the situation. You may want to just create a simple database (one table, maybe 2, 3 columns) in Access and a new application to access that database. If you have the same issues, then more than likely, your connection component is the problem, if not, then there's some other issue possibly order of the columns, or column field types.

IMN logo majestic logo threadwatch logo seochat tools logo