如何将DataTable/DataSet转换为ObjectDataSource

5

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

示例:

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

    ObjectDataSource1.DataSource = dt; 
}

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

如果你在想,为什么不直接将DataTable分配给GridView;答案是:我们喜欢ODS + GridView组合所提供的自动排序功能。

所有在Google上搜索到的内容都是关于如何从ODS获取DT。我找不到任何关于如何将DT传入ODS的参考资料。似乎这是一个非常普遍的需求,因为来自ASP.NET 1.1的人们会有很多生成DT的代码,如果他们想要更新UI,他们就需要将DT传入ODS。

3个回答

1

试试这个:

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

1
什么是objDtSrcUsers?你能再多写几行吗? - Nipuna

1

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

请查看此博客文章http://www.superedge.net/2010/04/how-to-populate-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();

    }

希望能对您有所帮助 祝好, -Edge


1

类似这样:

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

 //TODO:Load your data in the set

  return ds.YourDataTableName;
}

这段代码是错误的,你假设YourDataTableName是一个类型,同时也是YourDataSet的成员名称。 - Oscar Cabrero
@oscar:当然可以,但为什么不使用集合中的强类型dataTable呢? - Glenn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接