【发布时间】:2015-08-25 08:10:40
【问题描述】:
我在 DBGrid 之外有 DBGrid 和“删除”按钮。我如何确定该用户在 DBGrid 中选择了一些字符串?因为如果表单打开并且在 DBGrid 中没有选择字符串,并且用户单击了“删除”按钮 - 我需要向他显示警告框“没有选择字符串!选择要删除的字符串。”
【问题讨论】:
标签: delphi c++builder
我在 DBGrid 之外有 DBGrid 和“删除”按钮。我如何确定该用户在 DBGrid 中选择了一些字符串?因为如果表单打开并且在 DBGrid 中没有选择字符串,并且用户单击了“删除”按钮 - 我需要向他显示警告框“没有选择字符串!选择要删除的字符串。”
【问题讨论】:
标签: delphi c++builder
您没有说明您的网格是否设置为使用 dgRowSelect 和/或 dgMultiSelect。如果是,则当前选定行的列表可通过 SelectedRows 属性作为书签列表使用。
如果没有选定的记录,则此列表将为空,因此:
if myGrid.SelectedRows.Count = 0 then
// Nothing selected!
【讨论】:
dgMultiSelect 为真时有效。然后用户可以删除多条记录。
您需要查看 DBGrid1.SelectedRows
procedure TForm24.Button1Click(Sender: TObject);
var
BookmarkList: TBookmarkList;
Bookmark: TBookmark;
i: Integer;
begin
BookmarkList := DBGrid1.SelectedRows;
if BookmarkList.Count = 0 then
ShowMessage('No strings selected! Select the string you want to delete')
else
begin
for i := 0 to BookmarkList.Count - 1 do
begin
ClientDataSet1.GotoBookmark(BookmarkList[i]);
ClientDataSet1.Delete;
end;
end;
end;
【讨论】:
DBGrid1->DataSource->DataSet->IsEmpty()。该字符串始终显示为 FALSE。即使在 DBGRID 中选择了一些字符串。
DBGrid1->SelectedRows->Count 总是显示 0。即使选择了行。 dgRowSelect 为真。我解决了这个问题,将 dgAlwaysShowSelected 更改为 TRUE。现在总是选择某行。