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.