Intersoft Support Center

Create WebGrid in Server-side programmatically

WebGrid provides events in Server-Side to enable user implementing several tasks.

In this topic, you will learn how to create WebGrid in Server-Side programmatically.

To create WebGrid in Server-Side programmatically

  1. Put the following code in Page_Load event. These codes are used to define WebGrid's settings and events.
    C# Copy ImageCopy Code
    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. Put the following code in OnInit event.
    C# Copy ImageCopy Code
    protected override void OnInit(EventArgs e)
    {
       base.OnInit(e);
    }
    

  3. Put the following code in InitializeDataSource Server-Side event of WebGrid. These codes are used to bind the Customers table to DataSource.
    C# Copy ImageCopy Code
    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. Put the following code in PrepareDataBinding Server-Side event of WebGrid. These codes are used to retrieve and display the table structure in WebGrid.
    C# Copy ImageCopy Code
    private void grid_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
    {
       if (!IsPostBack)
       {
          WebGrid grid = (WebGrid)sender;
          grid.RetrieveStructure();
       }
    }

  5. Put the following code in InitializeLayout Server-Side event of WebGrid. These codes are used to set the layout settings available in WebGrid. In this case, WebGrid is set to enable context menu, export and edit.
    C# Copy ImageCopy Code
    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;
    }
                            

  6. Run the project.
Previous Next