【问题标题】:How to convert a DataTable/DataSet into a ObjectDataSource如何将 DataTable/DataSet 转换为 ObjectDataSource
【发布时间】:2009-03-27 23:46:33
【问题描述】:

我有一个绑定到 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 属性。这就是我被卡住的原因。

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

Google 上的所有搜索都返回了如何从 ODS 获取 DT。我找不到关于如何将 DT 放入 ODS 的单一参考。这似乎是一个相当普遍的需求,因为来自 ASP.NET 1.1 的人会有很多生成 DT 的代码,如果他们想更新 UI,他们会希望将 DT 放入 ODS。

【问题讨论】:

    标签: .net data-binding


    【解决方案1】:

    类似这样的:

    public YourDataTableName GetData()
    {
     YourDataSet ds = new YourDataSet();
    
     //TODO:Load your data in the set
    
      return ds.YourDataTableName;
    }
    

    【讨论】:

    • 此代码错误,您假设 YourDataTableName 是一个类型,也是 YourDataSet 成员的名称
    • @oscar:当然可以,但是为什么不使用集合中的强类型数据表呢?
    【解决方案2】:

    您可以将数据包装在公共方法周围,然后在 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();
    
        }
    

    希望对你有帮助 干杯, -边缘

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

      • 什么是objDtSrcUsers?你能多写几行吗
      猜你喜欢
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      相关资源
      最近更新 更多