Implementing a SOA.UI web service for DataGrid is the simplest and most elegant way of providing data to a DataGrid instance. By simply implementing the methods needed for DataGrid to communicate with the back-end data store, you take care of all the wiring and lifecycle issues of how DataGrid connects to the data while neatly separating the presentation layer code from the back-end data fetching code. Furthermore this web service can be used simultaneously by both Web.UI for Ajax Grid as well as Silverlight DataGrid controls.
Implementing the Web Service
Start off by creating a new WCF service and adding its reference to your project (steps to do this can be found here). The newly created service needs to either implement the ISoaDataGridService
interface, or extend the SoaDataGridService.
In this example we will choose the latter option, which is a blank implementation of the interface that allows programmers to omit implementing methods that they will not use in their implementation. The code below shows our WCF service that only implements the Select method. This implementation is sufficient to provide the data for our DataGrid instance as well as handle paging and sorting operations:
[ C# ]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SoaDataGridMessageService : SoaDataGridService
{
private DataSet LoadData()
{
//connect to back-end data store
//execute query to retrieve the relevant DataSet that will be stored in the grid
}
public override SoaDataGridSelectResponse Select(SoaDataGridSelectRequest request)
{
DataSet oDS = LoadData();
DataView oView = oDS.Tables[0].DefaultView;
oView.Sort = request.Sortings.ToSqlString();
SoaDataGridSelectResponse response = new SoaDataGridSelectResponse();
List<List<object>> arMessages = new List<List<object>>();
for (int i = request.Offset; i < request.Offset + request.Count && i < oView.Count; i++)
{
// load data row
List<object> msg = new List<object>();
foreach (SoaDataGridColumn oColumn in request.Columns)
{
msg.Add(oView[i][oColumn.Name]);
}
arMessages.Add(msg);
}
response.ItemCount = oView.Count;
response.Data = arMessages;
return response;
}
}
The Select method implemented in the code above will be called each time a view of data is updated in the DataGrid on the client-side. This is especially usefull in conjunction with DataGrid's Callback mode of operation which needs to handle paging and sorting operations that the end user can perform. As such the Select method implemented above does the following:
- Connects to the back-end data store (via LoadData method) and retrieves all the reocrds that could potentially be shown in the DataGrid
- Sorts the records according to the sort criteria passed from the SoaDataGridSelectRequest
- Creates the response that will be sent back to the DataGrid
- Selects only the records requested by the current view based on the offset and number of records needed
- Sets the total number of data records in the response
- Sends the response
See Also
SOA UI Web Service with Grouping