﻿<?xml version="1.0" encoding="utf-8"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Intersoft Community - WebGrid Enterprise - Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><description /><generator>http://www.intersoftsolutions.com</generator><language>en</language><copyright>Copyright 2002 - 2015 Intersoft Solutions Corp. All rights reserved.</copyright><ttl>60</ttl><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Mon, 06 Sep 2010 05:44:59 GMT</pubDate><dc:creator>qq397472251</dc:creator><description>&lt;p&gt;&lt;span class="postusername"&gt;&lt;span style="color: #ff0000"&gt;thank you &lt;/span&gt;&lt;a class="contentlink" href="http://www.intersoftpt.com/Community/Users/yudi/"&gt;&lt;span style="color: #ff0000; font-size: 15px"&gt;Yudi &lt;/span&gt;&lt;/a&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="postusername"&gt;&lt;strong&gt;You are really a smart Man!!&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Mon, 06 Sep 2010 03:08:21 GMT</pubDate><dc:creator>yudi</dc:creator><description>&lt;p&gt;&lt;span style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;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.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;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.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;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.&lt;/span&gt;&lt;/p&gt;
&lt;ol style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;&lt;li&gt;Create a global variable that is used as a flag of Grid’s appearance status.&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;public static Boolean IsShowed = false;&lt;/pre&gt;
&lt;ol style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;&lt;li value="2"&gt;In Page_Load event, add an if-then condition that will initialize WebGrid only if the global variable mentioned in previous step (IsShowed) is true.&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;protected void Page_Load(object sender, EventArgs e)
{
    if (IsShowed)
        dynamicGridInit();
}&lt;/pre&gt;
&lt;ol style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;&lt;li value="3"&gt;Simply set the value of IsShowed global variable on ButtonClick event.&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;protected void Button1_Click(object sender, EventArgs e)
{
    IsShowed = true;
    dynamicGridInit();
}&lt;/pre&gt;
&lt;p&gt;&lt;span style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;I enclosed the modified version of your code as attachment. Please have the attached file tested on your end.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'segoe ui', 'sans-serif'; color: #1f497d; font-size: 9pt"&gt;Hope this helps.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Thu, 02 Sep 2010 22:26:13 GMT</pubDate><dc:creator>qq397472251</dc:creator><description>&lt;p&gt;Hi Yudi,&lt;/p&gt;
&lt;p&gt;I have Creat a Webgrid by your way~&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #c0504d"&gt;but when I click&amp;nbsp; "Refresh" of the webgrid , the&amp;nbsp; page also refresh!It seems&amp;nbsp; that it is very Bad!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to:&amp;nbsp;&amp;nbsp;click&amp;nbsp; "Refresh" of the webgrid ,but &amp;nbsp;the&amp;nbsp; page&amp;nbsp;doen not refresh?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Waiting for you on line!!!!!&lt;/p&gt;
&lt;p&gt;My c# Code(You may change your language to Chinese):&lt;br /&gt;&lt;/p&gt;&lt;pre&gt;        #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 &amp;#43;= new ISNet.WebUI.WebGrid.DataSourceEventHandler(grid_InitializeDataSource);
            grid.PrepareDataBinding &amp;#43;= new ISNet.WebUI.WebGrid.DataSourceEventHandler(grid_PrepareDataBinding);
            grid.InitializeLayout &amp;#43;= 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
&lt;/pre&gt;
</description></item><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Fri, 04 Jun 2010 17:20:28 GMT</pubDate><dc:creator>dwh168</dc:creator><description>&lt;p&gt;Thank you for your help! &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;I will check it.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;have a nice weekend!&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;David&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Fri, 04 Jun 2010 17:19:10 GMT</pubDate><dc:creator>yudi</dc:creator><description>&lt;p&gt;&lt;span style="font-family: 'segoe ui','sans-serif'; color: #1f497d; font-size: 9pt"&gt;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.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'segoe ui','sans-serif'; color: #1f497d; font-size: 9pt"&gt;For more detail, please check the attached sample and let us know whether it helps or not.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Create WebGrid(include columns) programmatically?</title><link>http://www.intersoftsolutions.com/Community/WebGrid/Create-WebGridinclude-columns-programmatically/</link><pubDate>Fri, 04 Jun 2010 17:10:46 GMT</pubDate><dc:creator>dwh168</dc:creator><description>&lt;p&gt;&lt;span&gt;Do you have any example showing that can create WebGrid(include 
columns) programmatically?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;span /&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;span /&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;David&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;</description></item></channel></rss>