The following example illustrates how to use a DataSet as the source for an ObjectDataSource component.
In the code snippet bellow a DataSet object is filled with three tables and then used for the DataSource property of the ObjectDataSource component. The DataMemeber property of the ObjectDataSource component is set to indicate to which one of the three tables to bind to. Additionally a sample calculated field is added which can be used in the report definition in the same way as a regular field.
CopyC#
private void Form5_Load(object sender, EventArgs e)
{
var objectDataSource = new Telerik.Reporting.ObjectDataSource();
objectDataSource.DataSource = GetAllData();
objectDataSource.DataMember = "Product";
objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber"));
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = objectDataSource;
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = report;
reportViewer1.ReportSource = reportSource;
reportViewer1.RefreshReport();
}
static DataSet GetAllData()
{
const string connectionString =
"Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";
string selectCommandText = "SELECT Name, ProductCategoryID FROM Production.ProductCategory;" +
"SELECT Name, ProductCategoryID FROM Production.ProductSubcategory;" +
"SELECT Name, ProductNumber FROM Production.Product;";
SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connectionString);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
dataSet.Tables[0].TableName = "ProductCategory";
dataSet.Tables[1].TableName = "ProductSubcategory";
dataSet.Tables[2].TableName = "Product";
return dataSet;
}
CopyVB.NET
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim objectDataSource As New Telerik.Reporting.ObjectDataSource()
objectDataSource.DataSource = GetAllData()
objectDataSource.DataMember = "Product"
objectDataSource.CalculatedFields.Add(New Telerik.Reporting.CalculatedField("FullName", GetType(String), ))
Dim report As New Telerik.Reporting.Report()
report.DataSource = objectDataSource
Dim reportSource As new InstanceReportSource
reportSource.ReportDocument = report
reportViewer1.ReportSource = reportSource
reportViewer1.RefreshReport()
End Sub
Shared Function GetAllData() As DataSet
Const connectionString As String = _
"Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True"
Dim selectCommandText As String = _
"SELECT Name, ProductCategoryID FROM Production.ProductCategory;" _
+ "SELECT Name, ProductCategoryID FROM Production.ProductSubcategory;" _
+ "SELECT Name, ProductNumber FROM Production.Product;"
Dim adapter As New SqlDataAdapter(selectCommandText, connectionString)
Dim dataSet As New DataSet()
adapter.Fill(dataSet)
dataSet.Tables(0).TableName = "ProductCategory"
dataSet.Tables(1).TableName = "ProductSubcategory"
dataSet.Tables(2).TableName = "Product"
Return dataSet
End Function