2009-03-27 25 views
5

Tengo un GridView vinculado a un ObjectDataSource. Tengo un método que devuelve una DataTable. ¿Cómo alimentar DataTable a ObjectDataSource para que GridView se actualice en el código?Cómo convertir un DataTable/DataSet en un ObjectDataSource

Ejemplo:

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

    ObjectDataSource1.DataSource = dt; 
} 

Sí, sé que el ODS no tiene propiedad DataSource. Es por eso que estoy atascado.

Si está pensando, ¿por qué no simplemente asigna GridView DataTable directamente; la respuesta es: nos gustan las capacidades de clasificación automática que ofrece el combo ODS + GridView.

Todas las búsquedas en Google han regresado son cómo obtener un DT de un ODS. No puedo encontrar una sola referencia sobre cómo obtener un DT en el ODS. Parecería que esta es una necesidad bastante común ya que las personas que vienen de ASP.NET 1.1 tendrán una gran cantidad de código que genera DT y si desean actualizar la interfaz de usuario, querrán llevar el DT al ODS.

Respuesta

1

Algo como esto:

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

//TODO:Load your data in the set 

    return ds.YourDataTableName; 
} 
+0

este código es incorrecto usted está asumiendo que YourDataTableName es un tipo y también el nombre de un miembro de YourDataSet –

+0

@oscar: por supuesto, pero ¿por qué no utilizar la tabla de datos de tipo stongly typed? rom el conjunto? – Glenn

1

se puede envolver sus datos en torno a un método público, a continuación, utilizar la firma del método en el constructor de la ObjectDataSource. Durante el proceso de enlace, realizará una llamada al método especificado y obtendrá sus datos.

echar un vistazo a esta entrada del blog 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(); 

    } 

espero que ayude Cheers, -Edge

1

Prueba esto:

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

¿Qué es objDtSrcUsers? puedes poner algunas líneas más – Nipuna

Cuestiones relacionadas