我尝试通过使用您知道的 WebPArt 制作 myGridView 组件:
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace MyGridView
{
public class MyGridView : WebPart
{
GridView gv;
protected override void CreateChildControls()
{
gv = new GridView();
gv.CssClass = "tablestyle";
this.Controls.Add(gv);
}
}
}
我在工具箱上添加了这个 GridView。一切都好。我想将我自己的 CSS 设计添加到我的 GridView 中。但是如果我把这个 gridView 从工具箱拖到 aspx 页面。如果我绑定(bind)我的数据源;数据源是不显示自己的。
像那个主程序:
protected void Page_Load(object sender, EventArgs e)
{
LoadData loaddata = new LoadData();
DataTable dt = loaddata.LoadSQL("conn", "sp_GetAllCategory");
MyGridView1.datas.... -----> i can not see DataSource why?
}
我想查看我的 GridView 数据源。如果我写绑定(bind)数据源。 MyGridView1.DataSource -----> 为什么我看不到 DataSource?
最佳答案
因为需要在控件中添加DataSource; Web 部件默认不支持(basedataboundcontrol 类定义了 DataSource 和 DataBind)。所以你需要添加这个:
public object DataSource
{
get
{
this.EnsureChildControls();
return gv.DataSource;
}
set
{
this.EnsureChildControls();
gv.DataSource = value;
}
}
通常,您必须调用 EnsureChildControls() 以便在包装网格的属性之前创建所有子控件,但我不确定您是否可以访问 EnsureChildControls。我认为是。
关于c# - 如何通过 Webpart 类连接 GridView 数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2358779/