title: Displaying CheckBox in Gridview DataBound based on varchar column
body: <p>SQL Server table structure:</p>
  
  <pre><code>ChapName        varchar(200)
  Status          varchar(1)
  </code></pre>
  
  <p><strong>Requirement</strong>:</p>
  
  <ul>
  <li>I want to display checkbox in an ASP.NET gridview from Visual Studio 2010
  if the value of status column is T, let it be checked and unchecked otherwise.
  but it shows only textbox.</li>
  <li>I have tried <code>&lt;asp:templatefield&gt;</code> and <code>&lt;asp:itemtemplate&gt;</code> but it throws error if I try to bind this checkbox.</li>
  <li>any sample code is required as I am beginner in this field.</li>
  </ul>
  
  <p>The code I tried:</p>
  
  <pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
              CheckBox c = (CheckBox)GridView1.FindControl("ChkStatus");
              TextBox TB = (TextBox)GridView1.FindControl("Status");
  
          //Response.Write(TB.Text);
              if (TB.Text == "T")
              {
                  c.Checked = true;
              }
              else
              {
                  c.Checked = false;
              }
      }
  </code></pre>
  
  <p>The error I got</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
    
    <p>Description: An unhandled exception occurred during the execution of the current web
    request. Please review the stack trace for more information about the
    error and where it originated in the code.     </p>
    
    <p>Exception Details:<br>
    System.NullReferenceException: Object reference not set to an instance
    of an object.</p>
  </blockquote>
  
  <p>Aspx markup:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="Comp,QTypeCode" DataSourceID="SDS_QType_Edit" 
                BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" 
                BorderWidth="1px" 
                CellPadding="4" ForeColor="Black" GridLines="Vertical" 
                AllowPaging="True" AllowSorting="True" 
                onselectedindexchanged="GridView1_SelectedIndexChanged" 
                onrowdatabound="GridView1_RowDataBound"&gt;
      &lt;AlternatingRowStyle BackColor="White" /&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowEditButton="True" ShowSelectButton="True" /&gt;
          &lt;asp:BoundField DataField="QTypeCode" HeaderText="QTypeCode" 
                          SortExpression="QTypeCode" InsertVisible="False" 
                          ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="Descr" HeaderText="Descr" SortExpression="Descr" /&gt;
          &lt;asp:CheckBoxField DataField="AnsReq" HeaderText="AnsReq" ReadOnly="True" 
                             SortExpression="AnsReq" /&gt;
          &lt;asp:CheckBoxField DataField="OptionPrint" HeaderText="OptionPrint" 
                             ReadOnly="True" SortExpression="OptionPrint" /&gt;
          &lt;asp:BoundField DataField="Status" HeaderText="Status" 
                          SortExpression="Status" /&gt;
          &lt;asp:BoundField DataField="TransDate" HeaderText="TransDate" 
                          SortExpression="TransDate" /&gt;
          &lt;asp:BoundField DataField="UserName" HeaderText="UserName" 
                          SortExpression="UserName" /&gt;
          &lt;asp:TemplateField HeaderText="Check Box" &gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID ="ChkStatus" Text="text" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
       &lt;/Columns&gt;
       &lt;EditRowStyle Wrap="False" /&gt;
       &lt;EmptyDataRowStyle Wrap="False" /&gt;
       &lt;FooterStyle BackColor="#CCCC99" /&gt;
       &lt;HeaderStyle Wrap="False" BackColor="#6B696B" Font-Bold="True" ForeColor="White" /&gt;
       &lt;PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /&gt;
       &lt;RowStyle Wrap="False" BackColor="#F7F7DE" /&gt;
       &lt;SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" 
                         Wrap="False" /&gt;
       &lt;SortedAscendingCellStyle BackColor="#FBFBF2" /&gt;
       &lt;SortedAscendingHeaderStyle BackColor="#848384" /&gt;
       &lt;SortedDescendingCellStyle BackColor="#EAEAD3" /&gt;
       &lt;SortedDescendingHeaderStyle BackColor="#575357" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>