We intend to achieve the following goals with our enhancements:
- Eliminate repetitive coding for the EditCommand, CancelCommand, and DeleteCommand.
- Simplify the coding for UpdateCommand by extracting user inputs from bound columns.
- Make use of data adapters to achieve code-free programming. The several data adapters and their configuration wizards is the closest thing to code-free programming offered by Microsoft in ASP.NET 1.x. Although data adapters were designed to synchronize data between DataSet and data sources, the data adapters contain the necessary commands as well as the mapping information we can use to retrieve data and update the data sources from our ASP.NET page.
- Use with any data source. In fact, the control does not use the System.Data.SqlClient namespace.
The DataGridHelper.cs file in the WebDataControls project contains the code for the enhanced DataGridHelper control.
The most important new property of the enhanced DataGridHelper control is the DataAdapter property. If the DataAdapter property is assigned, the DataGridHelper control will attempt to use the data adapter automatically.
In the following, we will show the events exposed by the DataGridHelper control and how they behave depending on whether the DataAdapter property is set.
||If the DataAdapter property is set, the DataGridHelper control will load the data automatically and the event will not fire. If the DataAdapter property is not set, it is necessary to handle the LoadData event and set the DataSource property of the event argument.|
||This event will fire when the UpdateCommand event of the DataGrid is fired and before the data is updated. The event argument has a few properties. The Values property contains the name/value pair extracted from the DataGrid. We can validate and modify the values. We can also extract additional values using the Item property if the DataGrid contains template columns. Set the Cancel property if we want to cancel the update.|
||If the DataAdapter property is not set, we need to handle this event to update the data. The event argument has a Values property that contains the values as name/value pairs. If the DataAdapter property is set and there is an update command, the DataGridHelper control will update the data automatically. The event will then fire after the update and we can use it to handle any error occurred.|
||If event will fire when the DeleteCommand event of the DataGrid is fired and before the data is deleted. I include this event mostly for completeness. This event could be used to check external conditions and cancel the delete if the conditions are not met.|
||If the DataAdapter property is not set, we need to handle this event to delete the data. If the DataAdapter property is set and there is a delete command, the DataGridHelper control will delete the data automatically. The event will fire after the delete and we can use it to handle any error occurred.|
The event arguments for all the events except the first event have an Item property and a DataKey property. The Item property points to the current DataGridItem of the DataGrid control. The DataKey property is of the type of object array. In this version, the array only has one element but we create it as an array so that we can support multiple-column keys in the future.
The following table contains the properties of the DataGridHelper:
||Alternate the sort direction on subsequent clicks of the same column header.|
||If this property is true and the data source is DataSet, DataTable or DataView, the DataGridHelper control will sort the data automatically.|
||If the DataAdapter property is assigned, the DataGridHelper control will attempt to use it to load, update and delete data. If not assigned, we have to handle the LoadData, UpdateData and DeleteData events. We can assign any object inherited from DbAdapter class to this property.|
||The DataGrid control to bind to. Without binding to a DataGrid control. The DataGridHelper control is useless.|
||If assigned, the DataGridHelper will automatically display the error message occurred during update or delete. It is possible to modify the message in the UpdateData and DeleteData event.|
||The current sort expression. This property is useful when the user of the control wants to soft the data themselves in the LoadData event.|
||If this property is True, the control will reset the page index whenever the sort expression is changed.|
The DataGridHelper control also exposes a single method.
||Refresh the data if we have post-back from another control.|
We also add a new component, DataGridHelperConnector, to the WebDataControl.dll. This component is used to connect a DataGridHelper control to a DataGrid control, a data adapter, and a Label control at design time. It was created entirely for the sake code-free programming.
In the next section, we will show how to use the enhanced DataGridHelper control.