2009-03-27 11 views
5

Mam GridView, który jest związany z ObjectDataSource. Mam metodę, która zwraca DataTable. Jak przekazać DataTable do ObjectDataSource, aby GridView był aktualizowany w kodzie?Jak przekonwertować DataTable/DataSet na ObjectDataSource

Przykład:

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

    ObjectDataSource1.DataSource = dt; 
} 

Tak, wiem, że ODS nie posiada właściwości DataSource. Dlatego utknąłem.

Jeśli myślisz, dlaczego nie wystarczy przypisać bezpośrednio do GridView DataTable; odpowiedź brzmi: Podoba nam się funkcja automatycznego sortowania oferowana przez kombinację ODS + GridView.

Wszystkie wyszukiwania w Google zostały zwrócone, jak uzyskać DT z ODS. Nie mogę znaleźć pojedynczego odniesienia, jak wprowadzić DT do ODS. Wydawać by się mogło, że jest to dość powszechna potrzeba, ponieważ ludzie pochodzący z ASP.NET 1.1 będą mieli dużo kodu, który generuje DT i jeśli chcą zaktualizować interfejs użytkownika, będą chcieli uzyskać DT w ODS.

Odpowiedz

1

coś takiego:

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

//TODO:Load your data in the set 

    return ds.YourDataTableName; 
} 
+0

ten kod jest źle zakładając, że jesteś YourDataTableName jest typem, a także nazwą użytkownika YourDataSet –

+0

@oscar: Oczywiście, ale dlaczego nie korzystać ze sztywno wpisanych dataTable f rom z zestawu? – Glenn

1

można owinąć wokół swoich danych metodą publiczną, a następnie użyć podpisu metody w konstruktorze ObjectDataSource. Podczas procesu powiązania wykona połączenie z określoną metodą, a otrzymasz swoje dane.

spojrzeć na tym blogu 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(); 

    } 

Mam nadzieję, że to pomaga Cheers, -Edge

1

Spróbuj tego:

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

czym jest objDtSrcUsers? możesz dodać kilka dodatkowych linii – Nipuna