Programmatically Creating Multiple Dynamic GridViews in a tabs structure

In this article I'll explain how to create Grid View programmatically in SharePoint custom web parts using in C#.NET / ASP.NET using C#.NET Code behind.

I got a requirement that I have to create GridView programmatically. In my scenario I have used Easy Responsive Tabs and Created tab structure by dynamically adding List Item to an Unordered List Item in Responsive tabs UI structure to create tabs and container div's to create content place holder. In the Responsive tab content each tab is having a Grid view, so I have to generate and display Grid View programmatically in each tab.

My requirement output will looks like below.

Feedback Survey by creating Dynamic GridView in Tabs

To Create Grid View Programmatically with ASP.Net using C#.NET we have to write the following code in code behind

  //Loading the Place holder div control
  //which is Created in ASP.Net Control File
  HtmlGenericControl parentdivcontrol =

  HtmlGenericControl divChildControl =
    (HtmlGenericControl)parentdivcontrol.FindControl("container" + position);

  if (divChildControl != null)
   //Create GridView Dynamically with Properties
   GridView grid = new GridView();
   grid.ID = "grid" + Convert.ToString(position);
   grid.AutoGenerateColumns = false;
   grid.EmptyDataText = "No data found";
   grid.BorderWidth = 0;
   grid.GridLines = GridLines.None;
   grid.BorderStyle = BorderStyle.None;
   grid.Attributes.Add("class", "table table-hover myClass");

   //adding row data bound event
   grid.RowDataBound += new GridViewRowEventHandler(gvFeedback_RowDataBound);

   //adding Image Column using ImageField and setting its Properties
   ImageField imgfldUserImage = new ImageField();
   imgfldUserImage.HeaderText = "";
   imgfldUserImage.DataImageUrlField = "UserImage";

   //adding Data Field Column using BoundField and setting its Properties
   BoundField boundfldMentorName = new BoundField();
   boundfldMentorName.DataField = "Name";
   boundfldMentorName.HeaderText = "Mentor Name";
   boundfldMentorName.HeaderStyle.CssClass = "User-Header-css";
   boundfldMentorName.ItemStyle.CssClass = "User-Item-css";

   //adding Button Column using ButtonField and setting its Properties
   ButtonField btnfldStatus = new ButtonField();
   btnfldStatus.DataTextField = "Status";
   btnfldStatus.HeaderText = "Status";
   btnfldStatus.ButtonType = ButtonType.Button;
   btnfldStatus.ShowHeader = false;
   btnfldStatus.ControlStyle.CssClass = "ms-button-emphasize";

   //adding control to Place holder

   //Loading the grid view data and binding to it
   BindGrid(web, grid.ID, strSkillType);
   divChildControl.InnerHtml = "<span> No Data found for Feedback </span>";
click here to know about How to Bind ASP.NET Grid View Using C#
  //Bind Gridview data to the grid
  private void BindGrid(SPWeb web, string gridName, string strSkill)
    GridView grid = (GridView)divTitle.FindControl(gridName);
    //your code to bind grid.

  //Row data bound event
  protected void gvInternFeedback_RowDataBound(object sender,
                   System.Web.UI.WebControls.GridViewRowEventArgs e)
    //your code for RowDataBound event

Hope this information is useful for you.

Sudheer Gangumolu

I'm a Software Analyst / Developer / Blogger, having good experience in working with different flavors & Customization of SharePoint (i.e.,SP O365 / 2013 / 2010 / 2007) and .Net Apps. Currently, working on SharePoint 2013 / SharePoint O365. Guiding people on my technologies.

    Blogger Comment
    Facebook Comment