This example shows how to bind the ASP.NET Web Forms Dashboard Control to the Object Data Source and supply it with data using a custom fill service. To do this, you need to implement an IObjectDataSourceCustomFillService interface. In this example, the ObjectDataSourceFillParameters.Parameters collection of dashboard parameters is used to load the specified number of records.
public class CustomObjectDataSourceCustomFillService : IObjectDataSourceCustomFillService {
public object GetData(DashboardObjectDataSource dataSource, ObjectDataSourceFillParameters fillParameters) {
if (fillParameters.DataFields == null || fillParameters.DataFields.Length == 0) {
return null;
}
List<SalesPersonData> data = DataGenerator.Data;
DataTable table = new DataTable();
foreach (string field in fillParameters.DataFields) {
table.Columns.Add(field);
}
int NumberOfRecordsToRetrieveParameter = (int)(fillParameters.Parameters.FirstOrDefault(i => i.Name == "NumberOfRecords")?.Value);
int NumberOfRecordsToRetrieve = NumberOfRecordsToRetrieveParameter <= data.Count ? NumberOfRecordsToRetrieveParameter : data.Count;
for (int i = 0; i < NumberOfRecordsToRetrieve; i++) {
object[] row = new object[fillParameters.DataFields.Length];
for (int j = 0; j < fillParameters.DataFields.Length; j++) {
row[j] = GetPropertyValue(data[i], fillParameters.DataFields[j]);
}
table.Rows.Add(row);
}
return table.DefaultView;
}
object GetPropertyValue(SalesPersonData obj, string propName) {
return propName == "SalesPerson" ? (object)obj.SalesPerson : (object)obj.Quantity;
}
}
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
namespace DXWebApplication5 {
public partial class Default1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
ASPxDashboard1.SetDashboardStorage(dashboardFileStorage);
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
DashboardObjectDataSource objDataSource = new DashboardObjectDataSource("Object Data Source", typeof(SalesPersonData));
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml());
ASPxDashboard1.SetDataSourceStorage(dataSourceStorage);
ASPxDashboard1.ServiceContainer.AddService(typeof(IObjectDataSourceCustomFillService), new CustomObjectDataSourceCustomFillService());
}
}
}
Public Class CustomObjectDataSourceCustomFillService
Implements IObjectDataSourceCustomFillService
Public Function GetData(ByVal dataSource As DashboardObjectDataSource, ByVal fillParameters As ObjectDataSourceFillParameters) As Object Implements IObjectDataSourceCustomFillService.GetData
If fillParameters.DataFields Is Nothing OrElse fillParameters.DataFields.Length = 0 Then
Return Nothing
End If
Dim data As List(Of SalesPersonData) = DataGenerator.Data
Dim table As New DataTable()
For Each field As String In fillParameters.DataFields
table.Columns.Add(field)
Next field
Dim NumberOfRecordsToRetrieveParameter As Integer = CInt(Fix(If(fillParameters.Parameters.FirstOrDefault(Function(i) i.Name = "NumberOfRecords") Is Nothing, Nothing, fillParameters.Parameters.FirstOrDefault(Function(i) i.Name = "NumberOfRecords").Value)))
Dim NumberOfRecordsToRetrieve As Integer = If(NumberOfRecordsToRetrieveParameter <= data.Count, NumberOfRecordsToRetrieveParameter, data.Count)
For i As Integer = 0 To NumberOfRecordsToRetrieve - 1
Dim row(fillParameters.DataFields.Length - 1) As Object
For j As Integer = 0 To fillParameters.DataFields.Length - 1
row(j) = GetPropertyValue(data(i), fillParameters.DataFields(j))
Next j
table.Rows.Add(row)
Next i
Return table.DefaultView
End Function
Private Function GetPropertyValue(ByVal obj As SalesPersonData, ByVal propName As String) As Object
Return If(propName = "SalesPerson", DirectCast(obj.SalesPerson, Object), DirectCast(obj.Quantity, Object))
End Function
End Class
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System
Namespace DXWebApplication5
Partial Public Class Default1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim dashboardFileStorage As New DashboardFileStorage("~/App_Data/Dashboards")
ASPxDashboard1.SetDashboardStorage(dashboardFileStorage)
Dim dataSourceStorage As New DataSourceInMemoryStorage()
Dim objDataSource As New DashboardObjectDataSource("Object Data Source", GetType(SalesPersonData))
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml())
ASPxDashboard1.SetDataSourceStorage(dataSourceStorage)
ASPxDashboard1.ServiceContainer.AddService(GetType(IObjectDataSourceCustomFillService), New CustomObjectDataSourceCustomFillService())
End Sub
End Class
End Namespace