【问题标题】:can we assign objectdatasource control to a dataset?我们可以将 objectdatasource 控件分配给数据集吗?
【发布时间】:2012-08-09 18:07:41
【问题描述】:

我有两个问题:

  1. 我们可以将对象数据源控件分配给数据集吗?
  2. 我们能否使用对象数据源控件将两个或多个表返回到 gridview 或 detailsview。

我的主要关注点是我必须将对象数据源存储在数据集中,否则我的应用程序将需要进行大量更改。

【问题讨论】:

  • 怎么样?你能给我举个例子吗
  • 你基本上是在问如何绑定数据集?

标签: c# dataset datasource


【解决方案1】:

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

【讨论】:

  • 我不明白你的逻辑,但我的问题是如何将 objectdatasource 控件数据存储到数据集中
【解决方案2】:

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

如何将objectdatasource控件数据存储到数据集中

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

【讨论】:

    【解决方案3】:
    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;
    }
    

    【讨论】:

    • 我看到了这个例子。 var objectDataSource = new Telerik.Reporting.ObjectDataSource() 这个语句是什么意思
    • 通用 C# ObjectDataSource 类不支持将 DataSet 分配为 DataSource。在此示例中,作者使用第三方控件作为解决方案,因此这不是一个有效的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2011-02-11
    • 2010-11-29
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多