diff --git a/web/pgadmin/tools/datagrid/static/js/datagrid.js b/web/pgadmin/tools/datagrid/static/js/datagrid.js
index 02faaeef..84995d23 100644
--- a/web/pgadmin/tools/datagrid/static/js/datagrid.js
+++ b/web/pgadmin/tools/datagrid/static/js/datagrid.js
@@ -274,7 +274,7 @@ define('pgadmin.datagrid', [
            */
           var propertiesPanel = pgBrowser.docker.findPanels('properties');
           var queryToolPanel = pgBrowser.docker.addPanel('frm_datagrid', wcDocker.DOCK.STACKED, propertiesPanel[0]);
-
+          queryToolPanel.trans_id = trans_id;
           showQueryTool._set_dynamic_tab(pgBrowser, browser_preferences['dynamic_tabs']);
 
           // Set panel title and icon
diff --git a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
index ca9c3dae..93a7b296 100644
--- a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js
@@ -517,9 +517,10 @@ define('tools.querytool', [
       if (_.isNull(open_new_tab) || _.isUndefined(open_new_tab) || !open_new_tab.includes('qt')) {
         // Listen on the panel closed event and notify user to save modifications.
         _.each(pgWindow.default.pgAdmin.Browser.docker.findPanels('frm_datagrid'), function(p) {
-          if (p.isVisible()) {
+          if (p.trans_id == self.transId) {
+            self.currentPanel = p;
             p.on(wcDocker.EVENT.CLOSING, function() {
-              return self.handler.check_needed_confirmations_before_closing_panel(true, p);
+              return self.handler.check_needed_confirmations_before_closing_panel(true);
             });
 
             // Set focus on query tool of active panel
@@ -4908,7 +4909,7 @@ define('tools.querytool', [
 
       /* Checks if there is any unsaved data changes, unsaved changes in the query
       or uncommitted transactions before closing a panel */
-      check_needed_confirmations_before_closing_panel: function(is_close_event_call = false, panel= null) {
+      check_needed_confirmations_before_closing_panel: function(is_close_event_call = false) {
         var self = this, msg;
 
         /*
@@ -4950,7 +4951,7 @@ define('tools.querytool', [
           // No other function should call close() except through this function
           // in order to perform necessary checks
           self.ignore_on_close = undefined;
-          self.close(panel);
+          self.close();
         }
         // Return false so that the panel does not close unless close()
         // is called explicitly (when all needed prompts are issued).
@@ -5123,30 +5124,17 @@ define('tools.querytool', [
         alertify.confirmSave(gettext('Save changes?'), msg, is_unsaved_data);
       },
 
-      close: function(closePanel=null) {
+      close: function() {
         var self = this;
 
         pgBrowser.Events.off('pgadmin:user:logged-in', this.initTransaction);
-        // close specific panel
-        if (closePanel) {
-          self._close(closePanel);
-        } else {
-          // Iterate the all query tool instance and close the visible query tool instance.
-          _.each(pgWindow.default.pgAdmin.Browser.docker.findPanels('frm_datagrid'), function(panel) {
-            self._close(panel);
-          });
-        }
-      },
-      _close: function(closePanel) {
-        if (closePanel.isVisible()) {
-          window.onbeforeunload = null;
-          closePanel.off(wcDocker.EVENT.CLOSING);
-          // remove col_size object on panel close
-          if (!_.isUndefined(self.col_size)) {
-            delete self.col_size;
-          }
-          pgWindow.default.pgAdmin.Browser.docker.removePanel(closePanel);
+        window.onbeforeunload = null;
+        self.gridView.currentPanel.off(wcDocker.EVENT.CLOSING);
+        // remove col_size object on panel close
+        if (!_.isUndefined(self.col_size)) {
+          delete self.col_size;
         }
+        pgWindow.default.pgAdmin.Browser.docker.removePanel(self.gridView.currentPanel);
       },
       /* This function is used to raise notify messages and update
        * the notification grid.
