Hi @MJL1
My understanding is that you want to show or hide the Delete button in the subgrid. If that's correct, then you're right that it's not possible to achieve this with Power FX. The default Delete button's show/hide properties cannot be modified without using an external tool.
Even if you decide to build your own Delete button, it won't meet your requirements. If the command is configured in the main form, you won't be able to access the subgrid data. Conversely, if the command is configured in the subgrid table, you won't be able to access the main form data.
However you can actually achieve pretty much all you mentioned with a combination of command buttons and JS.
Lets start by creating the a JS file with the following code
// Function to Show/Hide Subgrid Based on Record Count (Form OnLoad Event)
function checkSubgridOnLoad(executionContext) {
var formContext = executionContext.getFormContext();
var subgridControl = formContext.getControl("Subgrid_new_1");
subgridControl.addOnLoad(function () {
var rows = subgridControl.getGrid().getRows();
if (rows.getLength() === 0) {
subgridControl.setVisible(false);
} else {
subgridControl.setVisible(true);
}
});
}
// Function to Toggle Subgrid Visibility (Manual Button)
function toggleSubgridVisibility(primaryControl) {
var formContext = primaryControl;
var subgridControl = formContext.getControl("Subgrid_new_1");
var currentVisibility = subgridControl.getVisible();
subgridControl.setVisible(!currentVisibility);
}
// Function to Delete All Records from Subgrid with Confirmation
function deleteSubgridRecords(primaryControl) {
var formContext = primaryControl;
var subgridControl = formContext.getControl("Subgrid_new_1");
if (confirm("Are you sure you want to delete all records?")) {
var rows = subgridControl.getGrid().getRows();
var deletePromises = [];
rows.forEach(function (row) {
var entityReference = row.getData().getEntity().getEntityReference();
var deletePromise = Xrm.WebApi.deleteRecord(entityReference.entityType, entityReference.id);
deletePromises.push(deletePromise);
});
Promise.all(deletePromises).then(
function success(results) {
console.log("All records deleted");
subgridControl.refresh(); // Refresh subgrid to reflect changes
subgridControl.setVisible(false); // Hide subgrid after deleting records
},
function error(error) {
console.log("Error deleting records: " + error.message);
}
);
}
}
// Ensure Subgrid is Visible When Manually Toggled Even if Initially Empty
function ensureSubgridVisibility(executionContext) {
var formContext = executionContext.getFormContext();
var subgridControl = formContext.getControl("Subgrid_new_1");
subgridControl.setVisible(true);
}Remember to change Subgrid_new_1 to the name of your subgrid before uploading your code.
in this link you can find all the information about how to create your file with the JS conde and configure it in the onload event of your main form.
https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/walkthrough-write-your-first-client-script
you should configure the function checkSubgridOnLoad in your OnLoad Event.

that is going to automatically show/hide the subgrid based on the number of items.
now the custom button, please edit your main form table command bar, select main Form and add a new command

name It as you want and in action select Run JavaScript, load the same library we create for the onload event, put the function toggleSubgridVisibility and add a new parameter then select PrimaryControl

Now for you delete button, please go and create a new Command this time in the Subgrids Table, and remember to select the Subgrid command bar.

put the function deleteSubgridRecords and add a new parameter then select PrimaryControl

And that’s it, I Hope evething is clear enough so you can follow the steps and achieve your requirement.
Feel free to ask any questions.
If my response resolved your issue, please feel free to mark it as the solution by clicking "Accept as solution." This helps others find the answer more easily. If you found the content helpful in any other way, a "Thumbs Up" would be greatly appreciated. Thank you!