如何将数据表/数据集转换为ObjectDataSource

时间:2022-08-25 23:27:39

I have a GridView that's tied to an ObjectDataSource. I have a method that returns a DataTable. How do I feed the DataTable to the ObjectDataSource so that the GridView is updated in code?

我有一个与ObjectDataSource绑定的GridView。我有一个返回DataTable的方法。如何将DataTable提供给ObjectDataSource,以便在代码中更新GridView ?

Example:

例子:

protected void Page_Load(object sender, EventArgs e) 
{
    MyClass obj = new MyClass(textbox.Text);
    DataTable dt = obj.GetData();

    ObjectDataSource1.DataSource = dt; 
}

Yes, I know that the ODS does not have DataSource property. That's why I'm stuck.

是的,我知道ODS没有数据源属性。这就是为什么我卡住了。

If you're thinking, why not just assign the GridView the DataTable directly; the answer is: We like the auto-sorting capabilities offered by the ODS + GridView combo.

如果您正在思考,为什么不直接将GridView指定为DataTable;答案是:我们喜欢ODS + GridView组合提供的自动排序功能。

All searches on Google have returned are how to get a DT from an ODS. I can't find a single reference on how to get a DT into the ODS. It would seem that this is quite a common need since people coming from ASP.NET 1.1 will have a lot of code that generates DT and if they want to update the UI, they will want to get the DT into the ODS.

在谷歌上返回的所有搜索都是如何从ODS中获取DT。我找不到一个关于如何把DT引入ODS的参考。这似乎是一个非常普遍的需求,因为人们来自ASP。NET 1.1将会有很多生成DT的代码,如果他们想要更新UI,他们会想要把DT输入到ODS中。

3 个解决方案

#1


1  

Something like this:

是这样的:

public YourDataTableName GetData()
{
 YourDataSet ds = new YourDataSet();

 //TODO:Load your data in the set

  return ds.YourDataTableName;
}

#2


1  

you can wrap your data around a public method, then use the signature of the method in the constructor of the ObjectDataSource. During the binding process it will perform a call to the method specified and you will get your data.

您可以围绕一个公共方法包装数据,然后在ObjectDataSource的构造函数中使用方法的签名。在绑定过程中,它将对指定的方法执行调用,您将获得数据。

take a look at this blog post http://www.superedge.net/2010/04/how-to-populate-objectdatasource.html

看看这篇博文http://www.superedge./2010/04/how-objectdatasource.html

/// <summary>

    /// Gets the data table for object source.

    /// </summary>

    /// <returns></returns>

    public DataSet GetDataTableForObjectSource()

    {

        // do whatever you want to do here and 

        // return the table with the data

        return MyDataSet.MyTable;

    }





    /// <summary>

    /// Called by the ASP.NET page framework to notify server controls that use 

    /// composition-based implementation to create any child controls 

    /// they contain in preparation for posting back or rendering.

    /// </summary>

    protected override void CreateChildControls()

    {

        base.CreateChildControls();



        // in this constructor specify the type name, the class name and 

        // the public method inside this class where the object datasource will retrieve the data

        // make it a signed assembly for security reasons.

        var edgeDataSource =

            new ObjectDataSource(

                "MyNamespace.MyClass, MyNamespace.MyClasss, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ce8ab85a8f42a5e8",

                "GetDataTableForObjectSource") {ID = "EdgeDataSource"};



        Grid = new SPGridView

                       {

                           AutoGenerateColumns = false,

                           AllowSorting = true,

                           AllowPaging = true,

                           AllowFiltering = true,

                           PageSize = 10

                       };



        // do not use DataSource property. MUST USE DataSourceID with the control name

        Grid.DataSourceID = "EdgeDataSource";



        // do this before the databind

        Controls.Add(edgeDataSource);

        Controls.Add(Grid);



        // bind the objects and execute the call 

        //specified in the ObjectDataSource constructor

        Grid.DataBind();

    }

I hope it helps Cheers, -Edge

我希望它能帮助大家欢呼,-Edge

#3


1  

Try this:

试试这个:

(objDtSrcUsers.Select() as DataView).Table.DataSet 

#1


1  

Something like this:

是这样的:

public YourDataTableName GetData()
{
 YourDataSet ds = new YourDataSet();

 //TODO:Load your data in the set

  return ds.YourDataTableName;
}

#2


1  

you can wrap your data around a public method, then use the signature of the method in the constructor of the ObjectDataSource. During the binding process it will perform a call to the method specified and you will get your data.

您可以围绕一个公共方法包装数据,然后在ObjectDataSource的构造函数中使用方法的签名。在绑定过程中,它将对指定的方法执行调用,您将获得数据。

take a look at this blog post http://www.superedge.net/2010/04/how-to-populate-objectdatasource.html

看看这篇博文http://www.superedge./2010/04/how-objectdatasource.html

/// <summary>

    /// Gets the data table for object source.

    /// </summary>

    /// <returns></returns>

    public DataSet GetDataTableForObjectSource()

    {

        // do whatever you want to do here and 

        // return the table with the data

        return MyDataSet.MyTable;

    }





    /// <summary>

    /// Called by the ASP.NET page framework to notify server controls that use 

    /// composition-based implementation to create any child controls 

    /// they contain in preparation for posting back or rendering.

    /// </summary>

    protected override void CreateChildControls()

    {

        base.CreateChildControls();



        // in this constructor specify the type name, the class name and 

        // the public method inside this class where the object datasource will retrieve the data

        // make it a signed assembly for security reasons.

        var edgeDataSource =

            new ObjectDataSource(

                "MyNamespace.MyClass, MyNamespace.MyClasss, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ce8ab85a8f42a5e8",

                "GetDataTableForObjectSource") {ID = "EdgeDataSource"};



        Grid = new SPGridView

                       {

                           AutoGenerateColumns = false,

                           AllowSorting = true,

                           AllowPaging = true,

                           AllowFiltering = true,

                           PageSize = 10

                       };



        // do not use DataSource property. MUST USE DataSourceID with the control name

        Grid.DataSourceID = "EdgeDataSource";



        // do this before the databind

        Controls.Add(edgeDataSource);

        Controls.Add(Grid);



        // bind the objects and execute the call 

        //specified in the ObjectDataSource constructor

        Grid.DataBind();

    }

I hope it helps Cheers, -Edge

我希望它能帮助大家欢呼,-Edge

#3


1  

Try this:

试试这个:

(objDtSrcUsers.Select() as DataView).Table.DataSet