簡體   English   中英

我們可以將objectdatasource控件分配給數據集嗎?

[英]can we assign objectdatasource control to a dataset?

我有兩個問題:

  1. 我們可以將objectdatasource控件分配給數據集嗎?
  2. 我們能否使用對象數據源控件將兩個或更多個表返回到gridview或detailsview。

我的主要重點是我必須將對象數據源存儲在數據集中,否則我的應用程序將需要進行很多更改。

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'沒有數據。 他只是返回一個方法的結果,這個方法應該返回一個IEnumerable。 IEnumerable可以是POCO,String,Int32等。

如何將對象數據源控件數據存儲到數據集中

如果可以返回“ System.Data.DataTable”,然后可以 ,在這種情況下,可以將其存儲在“ System.Data.DataSet”中。 但是對我來說,這毫無意義。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM