Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit and Update values in dbgrid in delphi

I am new to delphi and I am creating a database for customers and I am able to populate their details in a dbgrid by Viewcustomers button. However I am trying to update their details by: selecting a row and change a value in a cell and updating the same by click of edit button.

I am able to do the same by selecting the row and fething the data in a seperate form. But i would like to do change the values in dbgrid itself and update. Could someone help? Updated with TDB navigator and dgEnabled property.

a)MySQL b)TDatasource c) In a form, to update a customer i did like:

procedure TForm5.editCustomersButtonClick(Sender: TObject);
var i:integer;
begin
 for i:=0 to customersDataGrid.Columns.Count-1 do begin
  if customersDataGrid.Fields[i].FieldName='customerId' then 
        if customersDataGrid.Fields[i].FieldName='customerName' then customerNameValue.Text:=customersDataGrid.Fields[i].AsString;
        if customersDataGrid.Fields[i].FieldName='product' then productValue.Text:=customersDataGrid.Fields[i].AsString;
        if customersDataGrid.Fields[i].FieldName='city' then cityValue.Text:=customersDataGrid.Fields[i].AsString;
        if customersDataGrid.Fields[i].FieldName='country' then countryValue.Text:=customersDataGrid.Fields[i].AsString;
          begin
            try
              editCustomerQuery.Close;
              editCustomerQuery.Sql.Clear;
              editCustomerQuery.SQL.Add('UPDATE `employees_details`.`customers` SET `customerId`='''+customerIDValue1.Text+''', `customerName`='''+customerNameValue.Text+''', `product`='''+productValue.Text+''', `city`='''+cityValue.Text+''', `country`='''+countryValue.Text+''' WHERE `customerId`='+FloatToStr(customersDataGrid.Fields[i].AsFloat)+'');
              editCustomerQuery.Open;
              viewCustomerQuery.ApplyUpdates;
              viewCustomerQuery.Refresh;
              except on E : Exception do
            end;
          end;
        end;
        customerIDValue1.Text:='';
        customerNameValue.Text:='';
        productValue.Text:='';
        cityValue.Text:='';
        countryValue.Text:='';
        ShowMessage('Customer Changes have been updated in database');
        customersDataGrid.DataSource.DataSet.Refresh;
    end;

d) Just checking working of TDBedit in database demos it works, but i have created a new project with out any backend database still it doesn't allow me to edit.

like image 783
delsql Avatar asked Nov 20 '25 09:11

delsql


1 Answers

Yes, this is possible.

It's best to add a TDBNavigator to your form and set its DataSource to the same one as the grid. The point of doing this is that it gives you an easy way to save or cancel edits because of its buttons for those operations.

You should find that if you click twice in a cell in the grid, the first click focuses it and the second one puts it in edit mode. Then, you can edit the cell value in-place. If you want to avoid clicking the cell twice, set dgAlwaysShowEditor to True.

Btw, to enable in-place editing in the grid, the value dgEditing (under the grid's Options property in the Object Inspector) needs to be True and dgRowSelect needs to be False, as does the grid's ReadOnly property.

If you find that you can't edit a column value that you think ought to be editable, try the following:

  • If you've got persistent TFields set up for the dataset, check the TField for that column in the Object Inspector to make sure that the field isn't marked read-only.

  • Check that the dtataset doesn't have a read-only property that's set to True.

  • Put a TDBedit on the form and connect it to one of the dataset's fileds. Will that accept an edit.

  • If none of those things work, you'll just have to do some debugging. Inspect the grid's, fields' and datasets read-only properties at run-time using the debugger to make sure that none of them is True. OR start a new project consisting of only one form, a dataset, dataset, dbgrid and a dbnavigator with properties set as I've described: Now, can you in-place edit in that?

As an alternative to using the DBNavigator's Save and Cancel buttons, you can simply call DataSet.Post and DataSet.Cancel.

like image 196
MartynA Avatar answered Nov 22 '25 03:11

MartynA