title: Create multiple gridviews in code behind
body: <p>This is very interesting..</p>
  
  <p>I want to have multiple gridviews in a panel. and the number of gridviews is not fixed..</p>
  
  <p>So basically i think there should be no code in the .aspx page as i have to create the gridview in codebehind.</p>
  
  <p>I have the code for 1 gridview in one panel.. where i define the grid view in the HTML page and populate it from the code behind.</p>
  
  <p>Here is the code for that.. can any 1 please help me with the multiple gridviews...</p>
  
  <p>this is on the .aspx page</p>
  
  <pre><code>       &lt;asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" 
        AllowSorting="True" CellSpacing="2" onsorting="GridView1_Sorting"
                      Width="100%" ForeColor="White" GridLines="None" 
                        ondatabound="GridView1_DataBound1"&gt;
  
         &lt;Columns&gt;
                              &lt;EditItemTemplate&gt;
                                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("PolicyID") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/EditItemTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("PolicyID") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
        &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>this is the code behind</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              DataTable dt = new DataTable();
              SqlConnection connection = new SqlConnection(Session["ConnectionStringSQL"].ToString()); 
              connection.Open();
              SqlCommand sqlCmd = new SqlCommand("SELECT Policies.PolicyID, FROM Policies", connection);
              SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
  
              sqlDa.Fill(dt);
              connection.Close();
  
              if (dt.Rows.Count &gt; 0)
              {
                  for (int j = 0; j &lt; dt.Rows.Count; j++)
                  {
                      policyID.Add(dt.Rows[j]["PolicyID"].ToString());
  
                  }
                  taskTable.Columns.Add("PolicyID");
  
  
                  if (policyID.Count != 0)
                  {
                      for (int k = 0; k &lt; policyID.Count; k++)
                      {
                          DataRow tableRow = taskTable.NewRow();
  
                          tableRow["PolicyID"] = policyID[k];
  
                          taskTable.Rows.Add(tableRow);
                      }
  
                      Session["TaskTable"] = taskTable;
  
                      GridView1.DataSource = Session["TaskTable"];
                      GridView1.DataBind();
                  }
              }
          }
      }
  </code></pre>