title: Binding GridView to DataTable does not show data in the GridView, the GridView is empty
body: <p>I am working in ASP.Net with C# as code behind. The editor is Visual Studio 2012.
  I created a GridView with 5 columns: the first one contains checkboxes, the next 3 have text boxes and the last one is a buttonfield, as follows (I have made AutoGenerateColumns as false because, otherwise DataBind adds more columns":</p>
  
  <pre><code>&lt;asp:GridView ID="gvCDS" runat="server" AutoGenerateColumns="False"     OnRowDeleting="gvCDS_RowDeleting"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID="cbSelectedCDS" runat="server" Enabled="true"/&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Module Code"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="tbxInputCDSModuleCode" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Module Name"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="tbxInputCDSModuleName" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Credit Units"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="tbxInputCDSCreditUnits" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:ButtonField Text="Clear Module" /&gt;
      &lt;/Columns&gt;
  </code></pre>
  
  <p></p>
  
  <p>I need to show the GridView on selecting an item from a dropdown. The GridView always needs to have 8 rows. I have written the following code to show the GridView:</p>
  
  <pre><code>DataTable dt = ViewState["dt"] as DataTable;
  if (dt == null)
  {
      dt = new DataTable();
      dt.Columns.Add("", typeof(Boolean));
      dt.Columns.Add("Module Code", typeof(String));
      dt.Columns.Add("Module Name", typeof(String));
      dt.Columns.Add("Credit Units", typeof(String));
  
      for (int i = 0; i &lt; 8; i++)
      {
          dt.Rows.Add(dt.NewRow());
      }
  }
  gvCDS.DataSource = dt;
  gvCDS.DataBind();
  ViewState["dt"] = dt;
  gvCDS.Visible = true;
  </code></pre>
  
  <p>The user makes some inputs in the GridView and then clicks a button. In the button's click event procedure, I need to delete the checked rows from the GridView and add rows so that total number of rows remains as 8. If I simply call .DeleteRow method, the row is not getting deleted. I still see the GridView as earlier in the browser. So I first copy the content of GridView into DataTable as follows:</p>
  
  <pre><code>DataTable dt = ViewState["dt"] as DataTable;
  int rowCounter = 0;
  foreach (GridViewRow row in gvCDS.Rows)
  {
      DataRow dr = dt.Rows[rowCounter];
      dr[0] = ((CheckBox)row.FindControl("cbSelectedCDS")).Checked;
      dr[1] = ((TextBox)row.FindControl("tbxInputCDSModuleCode")).Text;
      dr[2] = ((TextBox)row.FindControl("tbxInputCDSModuleName")).Text;
      dr[3] = ((TextBox)row.FindControl("tbxInputCDSCreditUnits")).Text;
      rowCounter++;
  }
  </code></pre>
  
  <p>After that, instead of deleting rows from the GridView, I delete from the DataTable as follows:</p>
  
  <pre><code>rowCounter = 0;
  int rowsDeleted = 0;
  foreach (GridViewRow row in gvCDS.Rows)
  {
      if (((CheckBox)row.FindControl("cbSelectedCDS")).Checked)
      {
          dt.Rows.RemoveAt(rowCounter);
          rowsDeleted++;
      }
      else
      {
          rowCounter++;
      }  
  }
  
  rowsDeleted now contains the number of rows that were deleted. I add that many rows in the DataTable so that the total number of rows remains as 8.
  for (i = 0; i &lt; rowsDeleted; i++)
  {
      dt.Rows.Add(dt.NewRow());
  }
  </code></pre>
  
  <p>After that I bind GridView to DataTable as follows:</p>
  
  <pre><code>ViewState["dt"] = dt;
  gvCDS.DataSource = dt;
  gvCDS.DataBind();
  </code></pre>
  
  <p>The GridView still has 8 rows. That is correct. But the problem is that the entire GridView has become empty. When testing, I filled some of the rows and checked only one of them. So, the content of the unchecked rows should be there. But the entire GridView is empty. I do not know why.
  Not only that, I even tested by keeping all rows unchecked, which means no row will be deleted. Still, the GridView becomes all empty.
  How do I solve this problem?
  Thank you.</p>