从数据库中删除记录后 DBGrid 不刷新
DBGrid is not refreshing after deleting record from database
我想知道为什么我使用以下 MRE 删除数据库中的记录后我的 DBGridEh(后代 DBGrid)没有刷新。你会发现我在下面评论了 4 次尝试,但没有成功。删除基本有效,但 DBGrid 不更新或与数据源断开连接。
procedure TForm9.btnDeleteClick(Sender: TObject);
begin
//cds1.DisableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
uq1.SQL.Clear;
uq1.SQL.Text := 'DELETE FROM mymachine WHERE ListID = :Pid';
uq1.Params.ParamByName('Pid').Value := cds1.FieldByName('ListID').AsInteger;
uq1.ExecSQL;
//cds1.EnableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
//cds1.Active := False; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Active := True; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Close; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Open; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Refresh; //attempt 4 failed. "SQL Statement doesn't return rows." but it deletes and DBGrid data remains
end;
更新VCL.FILE
object dgh1: TDBGridEh
object ucn1: TUniConnection
ProviderName = 'mySQL'
Port = 3306
Database = 'manufacturingmngtsystem'
Username = 'root'
Server = 'localhost'
Connected = True
LoginPrompt = False
Left = 16
Top = 200
end
object mup1: TMySQLUniProvider
end
object uq1: TUniQuery
Connection = ucn1
SQL.Strings = (
'Select * From mymachine')
end
object dsp1: TDataSetProvider
DataSet = uq1
end
object cds1: TClientDataSet
Active = True
Aggregates = <>
Params = <>
ProviderName = 'dsp1'
object intgrfldcds1ListID: TIntegerField
FieldName = 'ListID'
end
object strngfldcds1Name: TStringField
FieldName = 'Name'
Required = True
Size = 36
end
object strngfldcds1Description: TStringField
FieldName = 'Description'
Size = 209
end
object strngfldcds1Status: TStringField
FieldName = 'Status'
Required = True
FixedChar = True
Size = 8
end
end
object ds1: TDataSource
DataSet = cds1
end
end
由于网格已连接到数据集 cds1
,因此一个简单的 cds1.Delete
即可。这之后可能必须调用 cds1.ApplyUpdates
以将更改转发到基础数据库。
我想知道为什么我使用以下 MRE 删除数据库中的记录后我的 DBGridEh(后代 DBGrid)没有刷新。你会发现我在下面评论了 4 次尝试,但没有成功。删除基本有效,但 DBGrid 不更新或与数据源断开连接。
procedure TForm9.btnDeleteClick(Sender: TObject);
begin
//cds1.DisableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
uq1.SQL.Clear;
uq1.SQL.Text := 'DELETE FROM mymachine WHERE ListID = :Pid';
uq1.Params.ParamByName('Pid').Value := cds1.FieldByName('ListID').AsInteger;
uq1.ExecSQL;
//cds1.EnableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
//cds1.Active := False; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Active := True; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Close; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Open; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Refresh; //attempt 4 failed. "SQL Statement doesn't return rows." but it deletes and DBGrid data remains
end;
更新VCL.FILE
object dgh1: TDBGridEh
object ucn1: TUniConnection
ProviderName = 'mySQL'
Port = 3306
Database = 'manufacturingmngtsystem'
Username = 'root'
Server = 'localhost'
Connected = True
LoginPrompt = False
Left = 16
Top = 200
end
object mup1: TMySQLUniProvider
end
object uq1: TUniQuery
Connection = ucn1
SQL.Strings = (
'Select * From mymachine')
end
object dsp1: TDataSetProvider
DataSet = uq1
end
object cds1: TClientDataSet
Active = True
Aggregates = <>
Params = <>
ProviderName = 'dsp1'
object intgrfldcds1ListID: TIntegerField
FieldName = 'ListID'
end
object strngfldcds1Name: TStringField
FieldName = 'Name'
Required = True
Size = 36
end
object strngfldcds1Description: TStringField
FieldName = 'Description'
Size = 209
end
object strngfldcds1Status: TStringField
FieldName = 'Status'
Required = True
FixedChar = True
Size = 8
end
end
object ds1: TDataSource
DataSet = cds1
end
end
由于网格已连接到数据集 cds1
,因此一个简单的 cds1.Delete
即可。这之后可能必须调用 cds1.ApplyUpdates
以将更改转发到基础数据库。