title: TemplateField in GridView Asp.Net Web Forms
body: <p>I have a problem with template fields where I create a link for data in a row in Grid View. When for a first time i run my page it works fine, but later when i hide some other columns in that grid view and i want to back for my first view (I show all columns that were hidden) mine code behind throw null pointer exception. But Query for that grid is the same and the columns are the same. It always happens when i hide and add some columns to my code.</p>
  
  <p>Please if u can help me with that i will be very grateful.</p>
  
  <pre><code>              &lt;asp:TemplateField HeaderText="zz" Visible="False"&gt;
                  &lt;ItemTemplate&gt;
                   &lt;asp:Label ID="Label_typ" runat="server"
                   Text='&lt;%# Eval("TYP") %&gt;'&gt;&lt;/asp:Label&gt; 
                      &lt;br&gt; &lt;/br&gt;
  
                   &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>Code behind witch complicates my all my code. Without it, it works fine.</p>
  
  <pre><code>        if (actualYear.Equals(dd_rok22.SelectedValue))
          {
              int numberOfWeek = 1;
              for (int i = Convert.ToInt32(actualMonth); i &lt;= 12; i++)
              {
                  GRID_VIEW_MAKRO.Columns[i + 7].Visible = false;
                  if (i == Convert.ToInt32(actualMonth))
                  {
                      int numberColumnActual = i;
                      BoundField field2 = new BoundField();
                      GRID_VIEW_MAKRO.Columns.Insert(numberColumnActual + 3, field2);
  
                      for (int j = 1; j &lt;= weekNumberActualMonth; j++)
                      {
                          BoundField field = new BoundField();
                          GRID_VIEW_MAKRO.Columns.Insert(numberColumnActual + 3, field);
  
                          field.HeaderText = "M" + actualMonth + "_T" + numberOfWeek;
                          field.DataFormatString = "{0:###,###}";
                          numberColumnActual++;
                          numberOfWeek++;
                      }
                      for (int numberOfWeekPrevious = numberOfWeek; numberOfWeekPrevious &lt;= 4; numberOfWeekPrevious++)
                      {
  
                          BoundField field = new BoundField();
                          GRID_VIEW_MAKRO.Columns.Insert(numberColumnActual + 2, field);
  
                          field.HeaderText = "M" + previousMonth2 + "_T" + numberOfWeek;
                          field.DataFormatString = "{0:###,###}";
                          numberOfWeek++;
                          numberColumnActual++;
                      }
  
                  }
              }
          }
  </code></pre>
  
  <p>And the exception shows when i create Label witch have null value because of TemplateField have null value.</p>
  
  <pre><code>protected void OnRowCreated(object sender, GridViewRowEventArgs e)
      {
  
  
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              string mregion= DataBinder.Eval(e.Row.DataItem, "MAKRO_NAME").ToString();
  
  
  
  
  
              Label lbl = (Label)e.Row.FindControl("Label_typ");
  
              string CellValue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ROK"));
  
  //it cannot Trim me null value..
              string ID = lbl2.Text.Trim() + '_' + lbl3.Text.Trim();
  
              e.Row.Attributes.Add("onclick", "top.location.href='report_se.aspx?nazwa=" + ID + "&amp;ye=" + CellValue + "';");
  
  
          }
      }
  </code></pre>