title: To disable linkbutton in grid view based on row value
body: <p>In the below code i have a grid view  which has 6 columns DocumentID ,Documentname,View,Edit,ViewButton,EditButton.My aim is if View is false ViewButton should be disable and if Edit is false editbutton to be disable.But i tried it throws null reference exception Pls help me to do this.</p>
  
  <pre><code>&lt;asp:GridView Width="100%" runat="server" ID="srchgrid" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="false" 
                    PageSize-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" OnPageIndexChanging="Search_PageIndex" AllowPaging="true" AllowSorting="true"  
                      CssClass="mGrid"
                      PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"  OnRowCommand="srchgrid_RowCommand" OnRowDataBound="srchgrid_RowDataBound" &gt;
                    &lt;Columns&gt;          
  
              &lt;asp:TemplateField HeaderText="Document ID" ItemStyle-Width="150px" Visible="false"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="DocumentID"  runat="server"  Text='&lt;%#Eval("DocumentID") %&gt;' &gt; &lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
  
  
              &lt;asp:TemplateField HeaderText="Document Name" ItemStyle-Width="200px"  &gt;              
                  &lt;ItemTemplate&gt;                                              
                      &lt;asp:Label ID="DocumentName"  runat="server"  Text='&lt;%#Eval("DocumentName") %&gt;'&gt; &lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;                
              &lt;/asp:TemplateField&gt;
  
               &lt;asp:TemplateField HeaderText="View" ItemStyle-Width="200px"   &gt;              
                  &lt;ItemTemplate&gt;                                              
                      &lt;asp:Label ID="View"  runat="server"  Text='&lt;%#Eval("ViewDoc") %&gt;'&gt; &lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;                
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Edit" ItemStyle-Width="200px"   &gt;              
                  &lt;ItemTemplate&gt;                                              
                      &lt;asp:Label ID="Edit"  runat="server"    Text='&lt;%#Eval("EditDoc") %&gt;'&gt; &lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;                
              &lt;/asp:TemplateField&gt;
                    &lt;asp:TemplateField HeaderText="ViewButton" itemstyle-width="150px"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:LinkButton ID="btnView" runat="server" CommandName="View" Text="View"  CausesValidation="false"/&gt;
                              &lt;/ItemTemplate&gt;    
                          &lt;/asp:TemplateField&gt; 
  
                           &lt;asp:TemplateField HeaderText="EditButton" itemstyle-width="150px"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:LinkButton ID="btnEdit" runat="server" CommandName="View1" Text="Edit" CausesValidation="false"/&gt;
                              &lt;/ItemTemplate&gt;    
                          &lt;/asp:TemplateField&gt;       
                         &lt;/Columns&gt;
  
                       &lt;HeaderStyle Font-Bold="True" ForeColor="White" /&gt;
      &lt;/asp:GridView&gt; 
  
  
      protected void srchgrid_RowDataBound(object sender, GridViewRowEventArgs e)
      {
         foreach (GridViewRow gr in srchgrid.Rows)
                  {
  
                      Label View = gr.Cells[2].Controls[2] as Label;
                      if (View.Text == "false")
                      {
                          LinkButton btnView = (LinkButton)gr.FindControl("btnView");
                          btnView.Enabled = false;
  
                      }
  
                  }
          foreach (GridViewRow gr in srchgrid.Rows)
              {
                  Label Edit = gr.Cells[3].Controls[3] as Label;
                  if (Edit.Text == "false")
                  {
                      LinkButton btnEdit = (LinkButton)gr.FindControl("btnEdit");
                      btnEdit.Enabled = false;
  
                  }
  
              }
  
      }
  </code></pre>