我有一个与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。
答案 0 :(得分:1)
这样的事情:
public YourDataTableName GetData()
{
YourDataSet ds = new YourDataSet();
//TODO:Load your data in the set
return ds.YourDataTableName;
}
答案 1 :(得分:1)
您可以围绕公共方法包装数据,然后在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();
}
我希望它有所帮助 干杯, 〜边缘
答案 2 :(得分:1)
试试这个:
(objDtSrcUsers.Select() as DataView).Table.DataSet