如何使用WebGrid在后台使用代码创建表格

作者:控件中国网   出处:控件中国网   2016-08-09 14:37:08   阅读:7

WebGrid Enterprise是Intersoft WebUI界面套包下的一款数据表格控件,该产品也可以单独使用和购买,WebGrid可以帮助开发人员通过设计器创建数据表格控件,也可以在后台使用代码创建,还可以使用客户端代码在客户端创建,下面主要讲述如何在后台使用代码创建数据表格:
 
1.放置下面的代码在Page_Load事件里,这些代码主要用于定义表格的设置和事件
protected void Page_Load(object sender, EventArgs e)
{
   WebGrid grid = new WebGrid();
   grid.ID = "WebGrid1";
   grid.Width = System.Web.UI.WebControls.Unit.Pixel(800);
   grid.Height = System.Web.UI.WebControls.Unit.Pixel(600);
   grid.UseDefaultStyle = true;
 
   grid.InitializeDataSource += new DataSourceEventHandler(grid_InitializeDataSource);
   grid.PrepareDataBinding += new DataSourceEventHandler(grid_PrepareDataBinding);
   grid.InitializeLayout += new LayoutEventHandler(grid_InitializeLayout);
 
   form1.Controls.Add(grid);
}
2、在OnInit事件里添加下面的代码
protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
}
3. 在表格的InitializeDataSource事件里进行数据的绑定
protected void grid_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
{
   dsNorthwind.CustomersDataTable dt = new dsNorthwind.CustomersDataTable();
   dsNorthwindTableAdapters.CustomersTableAdapter da = new dsNorthwindTableAdapters.CustomersTableAdapter();
   da.Fill(dt);
   e.DataSource = dt;
}
4.添加下面的代码到PrepareDataBinding事件里
private void grid_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
{  
   if (!IsPostBack)  
   {     
      WebGrid grid = (WebGrid)sender;
      grid.RetrieveStructure();
   }
}
5.在InitializeLayout事件里添加下面的代码,这些代码主要用于设置表格的布局等
protected void grid_InitializeLayout(object sender, ISNet.WebUI.WebGrid.LayoutEventArgs e)
{
   e.Layout.AllowContextMenu = true;
   e.Layout.AllowExport = Export.Yes;  
   e.Layout.AllowEdit = Edit.Yes;
}
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat