There are several methods to enhance the functionality of an existing control: to inherit from the control, to create a wrapper for the control or write an extender or helper class. We will use the last method because it preserves the design-time functionality of the DataGrid control.
The DataGridHelper.cs file in the WebDataControls project contains the code for the DataGridHelper control. The DataGridHelper control has a DataGrid property that can be used to attach a DataGridHelper control to a DataGrid control. The DataGridHelper then handles the SortCommand and IndexChanged events of the DataGrid so that users of the control do not have to handle these events. The DataGridHelper also handled the Page_Load type functionality in the OnPreRender event of the control. We use the OnPreRender event because it fires after the controls states are already restored but before the page is rendered.
The DataGridHelper control exposes a single event called LoadData. The users of DataGridHelper must respond to this event to get the data source and set it to the DataSource property of the event argument.
The DataGridHelper control also has a few additional properties that allow fine tuning its behavior:
||Alternate the sort direction on subsequent clicks of the same column header|
||If this property is true and the data source is a DataSet, DataTable or DataView, the DataGridHelper control will sort the data automatically|
||The current sort expression. This property is useful when the user of the control wants to sort 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 method, GridLoad. We need to call this method only if we have a post-back from another control and we want to refresh the data in response to the change.
In the next section, we will show how to use the DataGridHelper control.