iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Do you have any example showing that can create WebGrid(include columns) programmatically?
Thanks!
David
I enclosed one simple sample that shows how to create WebGrid programmatically as attachment. You can add the columns programmatically in OnPrepareDataBinding server side event of WebGrid.
For more detail, please check the attached sample and let us know whether it helps or not.
I have tested your snippet code in our local test and found difference between the sample file that I sent on my previous post in this thread and your code. My sample file put the grid initialization (InitializeDataSource; PrepareDataBinding; and InitializeLayout) in Page_Load event. Your code put the grid initialization in ButtonClick event.
When user clicks the refresh button of WebGrid, by default Page_Load event will be invoked. In my sample, when user clicks the refresh button of WebGrid, the grid remains on the page. In your code, when user clicks the refresh button of WebGrid, the grid will disappear.
I have a suggestion for your scenario. I assume that the ASP.NET button on your code is used to show WebGrid to the form. If my assumption is correct, I would like to suggest you to follow this workaround.
public static Boolean IsShowed = false;
protected void Page_Load(object sender, EventArgs e) { if (IsShowed) dynamicGridInit(); }
protected void Button1_Click(object sender, EventArgs e) { IsShowed = true; dynamicGridInit(); }
I enclosed the modified version of your code as attachment. Please have the attached file tested on your end.
Hope this helps.
Thank you for your help!
I will check it.
have a nice weekend!
Hi Yudi,
I have Creat a Webgrid by your way~
but when I click "Refresh" of the webgrid , the page also refresh!It seems that it is very Bad!
How to: click "Refresh" of the webgrid ,but the page doen not refresh?
Waiting for you on line!!!!!
My c# Code(You may change your language to Chinese):
#region dynamicCreateWebgrid protected void Button11_Click1(object sender, EventArgs e) { dynamicGridInit(); } public void dynamicGridInit() { WebGrid grid = new WebGrid(); grid.ID = "WebGrid5"; grid.Width = System.Web.UI.WebControls.Unit.Pixel(800); grid.Height = System.Web.UI.WebControls.Unit.Pixel(100); grid.UseDefaultStyle = true; grid.InitializeDataSource += new ISNet.WebUI.WebGrid.DataSourceEventHandler(grid_InitializeDataSource); grid.PrepareDataBinding += new ISNet.WebUI.WebGrid.DataSourceEventHandler(grid_PrepareDataBinding); grid.InitializeLayout += new ISNet.WebUI.WebGrid.LayoutEventHandler(grid_InitializeLayout); form1.Controls.Add(grid); } private void grid_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { //if (!IsPostBack) //{ WebGrid grid = (WebGrid)sender; grid.RetrieveStructure(); //} } protected void grid_InitializeLayout(object sender, ISNet.WebUI.WebGrid.LayoutEventArgs e) { WebGrid grid = (WebGrid)sender; grid.LayoutSettings.AllowContextMenu = true; grid.LayoutSettings.AllowExport = Export.Yes; grid.LayoutSettings.AlternatingColors = false; grid.LayoutSettings.AllowSelectColumns = SelectColumns.Yes; grid.LayoutSettings.AllowEdit = Edit.Yes; grid.LayoutSettings.StatusBarStyle.BackColor = System.Drawing.Color.Chocolate; } protected void grid_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { e.DataSource = mytb(); } public DataSet mytb() { DataSet ds = new DataSet("tb_Employee"); ds.ExtendedProperties["TimerStamp"] = DateTime.Now; ds.ExtendedProperties["Company"] = "www.sina.com"; DataColumn EmployeeIDColumn = new DataColumn("EmployeeID", typeof(int)); EmployeeIDColumn.Caption = "员工编号"; EmployeeIDColumn.ReadOnly = true; EmployeeIDColumn.AllowDBNull = false; EmployeeIDColumn.Unique = true; EmployeeIDColumn.AutoIncrement = true; EmployeeIDColumn.AutoIncrementSeed = 0; EmployeeIDColumn.AutoIncrementStep = 1; DataColumn EName = new DataColumn("EName", typeof(string)); DataColumn ESex = new DataColumn("ESex", typeof(string)); DataColumn EAge = new DataColumn("EAge", typeof(int)); DataColumn EPlace = new DataColumn("EPlace", typeof(string)); DataColumn EMoney = new DataColumn("EMoney", typeof(int)); DataTable dt = new DataTable("tb_Employee"); dt.Columns.AddRange(new DataColumn[] { EmployeeIDColumn, EName, ESex, EAge, EPlace, EMoney }); DataRow dr = dt.NewRow(); dr["EName"] = "huangjshhhh"; dr["ESex"] = "男"; dr["EAge"] = 20; dr["EPlace"] = "上海"; dr["EMoney"] = 10000; dt.Rows.Add(dr); dr = dt.NewRow(); dr["EName"] = "chengshuang"; dr["ESex"] = "女"; dr["EAge"] = 20; dr["EPlace"] = "北京"; dr["EMoney"] = 10000; dt.Rows.Add(dr); dt.PrimaryKey = new DataColumn[] { dt.Columns[0] }; ds.Tables.Add(dt); return ds; } #endregion
thank you Yudi :
You are really a smart Man!!
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname