delphidelphi-7dbgridtadotable

Select field from dbgrid


I have a DBGrid that is linked to DataSource (that is linked to TADOTable). All this runs through a TADOConnection and connects to a MS Access

The grid contains various values and I would like to edit it when I click on a specific field. I'm not experiencing any difficulty updating normal text fields, however I can't get a way to edit fields that contain an OLE Object.

What I want to do is, when I click a field, I want a open dialog box to open and let me select a file. After that, the file that I selected, must be updated to the field of the row I selected.

How would I go about doing this?


Solution

  • Figured it out myself. Posting it here so that others can benefit. I just went to my DBGrid and in object inspector, under events, I selected OnCellClick:

    procedure TfrmOne.dbgOneCellClick(Column: TColumn);
    begin
    var
      line : integer;
    begin
      line := DataSource1.DataSet.FieldValues['ID'];
    

    As you can see, line gets the field value of the selected row, in this case the ID. Each row has its unique ID (Primary Key), linking/coming from the database. With this primary key, I did a simple IF statement:

    tblOne.Close;
    tblOne.Open;
    tblOne.First
    while not tblOne.Eof do
    if tblOne['ID'] = line then
    begin
     if OpenDialog1.Execute then
      {*Insert Code here*}
     Exit;
    end else
     tblOne.Next;
    end;
    

    Basically what happens in the second piece of code is, the ID is matched to the current table row. If it does not match, the database moves on one row, until it is eventually a match. From here one can specifically work with that row (as originally seen/selected in the DBGrid) and update fields (OLE Object).

    This is not a replacement for the Append or Edit command, it merely allows a user to execute a opendialog and then save those files to the selected field in the DBGrid. Take note, for adding photos to the database (jpeg), you will need blobstreams etc. A full guide can be found here: http://delphi.about.com/od/database/l/aa030601a.htm