我们可以将objectdatasource控件分配给数据集吗?

时间:2012-08-09 18:07:42

标签: c# dataset datasource

我有两个问题:

  1. 我们可以将objectdatasource控件分配给数据集吗?
  2. 我们可以使用对象数据源控件将两个或多个表返回到gridview或detailsview。
  3. 我主要关注的是我必须将对象数据源存储在数据集中,否则我的应用程序将需要进行大量更改。

3 个答案:

答案 0 :(得分:0)

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];

答案 1 :(得分:0)

'ObjectDataSource'没有数据。 他只是返回一个方法的结果,这个方法应该返回一个IEnumerable。 IEnumerable可以是POCO,String,Int32等。

  

如何将objectdatasource控制数据存储到数据集

如果您返回“System.Data.DataTable”,然后,则可能会将其存储在“System.Data.DataSet”中。但对我来说,这没什么意义。

答案 2 :(得分:-1)

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;
}