How to convert a DataTable / DataSet to an ObjectDataSource

I have a GridView attached to an ObjectDataSource. I have a method that returns a DataTable. How can I pass a DataTable to an ObjectDataSource so that the GridView is updated in code?

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 ODS does not have the DataSource property. That is why I am stuck.

If you are thinking, why not just assign a GridView DataTable directly; the answer is: we like the automatic sorting capabilities offered by the ODS + GridView combine.

All google searches are back, how to get DT from ODS. I can not find any links on how to get DT in ODS. This would seem to be a fairly common need, since people coming from ASP.NET 1.1 will have a lot of code that DT generates, and if they want to update the user interface, they will want to get DT in ODS.

+5
source share
3 answers

Something like that:

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

 //TODO:Load your data in the set

  return ds.YourDataTableName;
}
+1
source

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

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();

    }

, , -

+1

Try the following:

(objDtSrcUsers.Select() as DataView).Table.DataSet 
+1
source

All Articles