views:

2090

answers:

2

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?

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.

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.

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.

A: 

Something like this:

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

 //TODO:Load your data in the set

  return ds.YourDataTableName;
}
Glenn
this code is wrong you are assuming that YourDataTableName is a type and also the name of a member of YourDataSet
Oscar Cabrero
@oscar: Of course but why not use the stongly typed dataTable from the set?
Glenn
A: 

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.

take a look at this blog post 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();

    }

I hope it helps Cheers, -Edge

Edge