title: Displaying GridView at Page Load
body: <p>I would like to manually add rows to a GridView, and display it at Page Load. For some reason, my current code shows an empty GridView.</p>
  <p><strong>Default.aspx</strong></p>
  <pre><code>     &lt;asp:GridView ID=&quot;AllocationGridView&quot; runat=&quot;server&quot; ShowHeaderWhenEmpty=&quot;true&quot; AutoGenerateColumns=&quot;False&quot;
                          Width=&quot;691px&quot; CellPadding=&quot;4&quot; ForeColor=&quot;#333333&quot; GridLines=&quot;None&quot; AllowSorting=&quot;True&quot;
                          AutoPostBack=&quot;true&quot;&gt;
                          &lt;AlternatingRowStyle BackColor=&quot;White&quot; ForeColor=&quot;#284775&quot; Height=&quot;2px&quot; /&gt;
                          &lt;Columns&gt;
                              &lt;asp:TemplateField HeaderText=&quot;Asset Class&quot;&gt;
                                  &lt;ItemStyle Font-Size=&quot;13px&quot; Width=&quot;20%&quot; /&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID=&quot;AssetLabel&quot; runat=&quot;server&quot; ReadOnly=&quot;true&quot; Text=&quot;&quot; BorderWidth=&quot;0px&quot;
                                          Style=&quot;text-align: left;&quot;&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText=&quot;Weight&quot;&gt;
                                  &lt;ItemStyle Font-Size=&quot;13px&quot; Width=&quot;20%&quot; /&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:TextBox ID=&quot;WeightTextBox&quot; runat=&quot;server&quot; ReadOnly=&quot;true&quot; BorderWidth=&quot;0px&quot;
                                          Style=&quot;text-align: left;&quot;&gt;&lt;/asp:TextBox&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                          &lt;/Columns&gt;
                          &lt;EditRowStyle BackColor=&quot;#EBEBEB&quot; /&gt;
                          &lt;FooterStyle BackColor=&quot;#5D7B9D&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt;
                          &lt;HeaderStyle BackColor=&quot;#5D7B9D&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt;
                          &lt;PagerStyle BackColor=&quot;#284775&quot; ForeColor=&quot;White&quot; HorizontalAlign=&quot;Center&quot; /&gt;
                          &lt;RowStyle BackColor=&quot;#F7F6F3&quot; ForeColor=&quot;#333333&quot; /&gt;
                          &lt;SelectedRowStyle BackColor=&quot;#E2DED6&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#333333&quot; Height=&quot;10px&quot; /&gt;
                          &lt;SortedAscendingCellStyle BackColor=&quot;#E9E7E2&quot; /&gt;
                          &lt;SortedAscendingHeaderStyle BackColor=&quot;#506C8C&quot; /&gt;
                          &lt;SortedDescendingCellStyle BackColor=&quot;#FFFDF8&quot; /&gt;
                          &lt;SortedDescendingHeaderStyle BackColor=&quot;#6F8DAE&quot; /&gt;
                      &lt;/asp:GridView&gt;
  </code></pre>
  <p><strong>Default.aspx.cs</strong></p>
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              FirstAllocationGridViewRow();
          }           
  
      }
  
      protected void FirstAllocationGridViewRow()
      {
          DataTable table = new DataTable();
  
          string[] assets = new string[] { &quot;Cash&quot;, &quot;US Equity&quot;, &quot;Fixed Income&quot;, &quot;BAS&quot;, &quot;International&quot; };
  
          table.Columns.Add(new DataColumn(&quot;Col1&quot;, typeof(string)));
          table.Columns.Add(new DataColumn(&quot;Col2&quot;, typeof(double)));
  
          DataRow dr = table.NewRow();
  
     
          for (int i = 0; i &lt; assets.Count(); i++)
          {
              dr = table.NewRow();
  
              dr[&quot;Col1&quot;] = assets[i];
              dr[&quot;Col2&quot;] = DBNull.Value;
  
              table.Rows.Add(dr);
          }
  
          ViewState[&quot;currentAllocationTable&quot;] = table;
  
          AllocationGridView.Visible = true;
          AllocationGridView.DataSource = table;
          AllocationGridView.DataBind();
      }
  </code></pre>