我有两个问题:
- 我们可以将 objectdatasource 控件分配给数据集吗?
- 我们可以使用对象数据源控件将两个或多个表返回到 gridview 或 detailsview。
我的主要关注点是我必须将对象数据源存储在数据集中,否则我的应用程序将需要进行大量更改。
我有两个问题:
我的主要关注点是我必须将对象数据源存储在数据集中,否则我的应用程序将需要进行大量更改。
“ObjectDataSource”没有数据。他只是简单的返回一个方法的结果,这个方法应该返回一个IEnumerable。IEnumerable 可以是 POCO、String、Int32 等。
如何将对象数据源控件数据存储到数据集中
如果您返回“System.Data.DataTable”,然后,是的,在这种情况下,您可以将其存储在“System.Data.DataSet”中。但对我来说,意义不大。
1 是的,您可以将 objectdatasource 分配给 DataSet
private DataSet GetDataSet(ObjectDataSource ods)
{
var ds = new DataSet();
var dv = (DataView)ods.Select();
if (dv != null && dv.Count > 0)
{
var dt = dv.ToTable();
ds.Tables.Add(dt);
}
return ds;
}
2 是的,您可以返回很多表,但是当您绑定时,您需要指定索引表。
GridView1.DataSource = YourDataSet.Tables[0];
GridView2.DataSource = YourDataSet.Tables[2];
private void Form5_Load(object sender, EventArgs e)
{
// Creating and configuring the ObjectDataSource component:
var objectDataSource = new Telerik.Reporting.ObjectDataSource();
objectDataSource.DataSource = GetAllData(); // GetData returns a DataSet with three tables
objectDataSource.DataMember = "Product"; /// Indicating the exact table to bind to. If the DataMember is not specified the first data table will be used.
objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber")); // Adding a sample calculated field.
// Creating a new report
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
// Assigning the ObjectDataSource component to the DataSource property of the report.
report.DataSource = objectDataSource;
// Use the InstanceReportSource to pass the report to the viewer for displaying
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = report;
// Assigning the report to the report viewer.
reportViewer1.ReportSource = reportSource;
// Calling the RefreshReport method (only in WinForms applications).
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();
// The data set will be filled with three tables: ProductCategory, ProductSubcategory
// and Product as the select command contains three SELECT statements.
adapter.Fill(dataSet);
// Giving meaningful names for the tables so that we can use them later.
dataSet.Tables[0].TableName = "ProductCategory";
dataSet.Tables[1].TableName = "ProductSubcategory";
dataSet.Tables[2].TableName = "Product";
return dataSet;
}