An example of a data grid that queries and updates data in SQL.
This example uses Invoke-DbaQuery from dbatools to query and update data in a SQL database. It implements paging, sorting and filtering via the data grid's -LoadRows event handler. It also renders buttons within the table that updates the database based on selections.
Additionally, bulk actions are performed using checkbox selection.
The -LoadRows event handler does much of the work but custom columns also provide an avenue for updating the database. We'll break down this example below.
Columns
The columns used in this example are basic, aside from a decision column that is used to update the SQL database.
The basic columns to display look like this. Properties that are returned from the SQL query will be available in render methods but will not be displayed.
We will implement a complex column to update the SQL database. The $EventData variable will include the entire row's data. We can use this data to render buttons based on its state. If the certificate is approved, we will create green buttons. If the certificate is revoked, we will create red buttons. Clicking the buttons will run an UPDATE command and then reload the dynamic region.
Filtering
Filtering is performed by accessing the $EventData variable (assigned to $Context in this example). Filters are available as a property of the context. Filters also include the logic operator used. This will be a and or or operator. We construct a SQL where expression and populate a hashtable of parameters.
The ConvertTo-ProperCase function adjusts the field name, which will be lower case, into the correct case used for the SQL server columns.
Paging
Paging data is also sent via the context. We can calculate the offset and page size using the following code.
Sorting
Columns used for sorting and their direction are also included in the context. This example uses single column sorting but the sort value is an array if multi-column sort is enabled.
Execution
Once we have constructed the SQL query portions and the parameters from the context, we can invoke our query. First, we need to get a total count of rows.
Once we have our count, the next step is to run the query. We return the rows as PSObjects. We need to format the return data to include the rows and the count.
Bulk Actions
Using the selection feature of the data grid, we can record the row selections and then perform bulk actions using a select dropdown.
The first step is to store the selections in session state. The $EventData variable of OnSelectionChanged is a list of the IDs of the rows. The data grid requires an ID to be defined for each row. If no ID property is found on a row, a random one is generated. Since we want deterministic selections, we use the CertificateID property as an ID in the select to allow for us to use that in queries. Below we store the selected IDs in a session variable and update a label for the select dropdown.
The select drop down uses the selections and then issues an UPDATE command for each of this. This could be optimized to execute a single command with a list of IDs. Once the code the OnChange is run, the table is updated and the drop down is refreshed.
Demo
Below is a demo of this app.
Demo of SQL Data Grid
Complete Example
The entire implementation of the app can be found below.