title: GridView RowDataBound Handler - Can't get data from row
body: <p>I have a GridView with an anonymous type. I need to check the value in a cell and highlight that cell if a condition is met. The problem is, I always get an empty string when I try to pull data out of the row's cells. I have successfully highlighted all the cells and I have checked in the Visual Studio 2010 debugger and confirmed that the Row has data (the row's DataItem has the values I need). This is happening on a PostBack, I'm not sure if that's a problem or not.</p>
  
  <p>Here is the code and solutions I've tried:</p>
  
  <pre><code>protected void grvValidCourses_RowDataBound(Object sender, GridViewRowEventArgs e) {
   if (e.Row.RowType == DataControlRowType.DataRow) {
    String str = e.Row.Cells[6].Text.ToString(); // empty string
    Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
    DataRowView rowView = (DataRowView)e.Row.DataItem; // exception about casting anonymous type
  </code></pre>
  
  <p>What's going on here? Why can't I get data from the cells?</p>
  
  <p>Markup for GridView:</p>
  
  <pre><code> &lt;asp:GridView ID="grvValidCourses" runat="server" Width="790px" OnRowCancelingEdit="grvValidCourses_RowCancelingEdit"
                      OnRowEditing="grvValidCourses_RowEditing" OnRowUpdating="grvValidCourses_RowUpdating" OnRowDeleting="grvValidCourses_RowDeleting"
                      AutoGenerateColumns="False" OnSelectedIndexChanged="grvValidCourses_SelectedIndexChanged"
                      OnRowDataBound="grvValidCourses_RowDataBound" &gt;
                      &lt;Columns&gt;
                          &lt;asp:CommandField ShowEditButton="True" EditText="Edit" UpdateText="Update |" /&gt;
                          &lt;asp:TemplateField ShowHeader="False"&gt;
                               &lt;ItemTemplate&gt;
                                 &lt;asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
                                      Text='&lt;%# (Eval("active") == null ? "Delete" : ((Eval("active").ToString() == "0" ? "Restore" : "Delete"))) %&gt;' /&gt;  
                               &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:CommandField ShowSelectButton="True" SelectText="Details" /&gt;
                          &lt;asp:TemplateField HeaderText="Training Name" SortExpression="coursename"&gt;
                              &lt;EditItemTemplate&gt;
                                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("coursename") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/EditItemTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Bind("coursename") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:BoundField DataField="ttNo" HeaderText="#" SortExpression="ttNo" ReadOnly="True" /&gt;
                          &lt;asp:TemplateField HeaderText="Course Date" SortExpression="startDate"&gt;
                              &lt;EditItemTemplate&gt;
                                  &lt;asp:TextBox ID="TextBox3" runat="server" Text='&lt;%# Bind("startdate", "{0:M/d/yyyy}") %&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;asp:CalendarExtender ID="TextBox3_CalendarExtender" runat="server" Enabled="True"
                                      TargetControlID="TextBox3" Format="M/d/yyyy"&gt;
                                  &lt;/asp:CalendarExtender&gt;
                              &lt;/EditItemTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="Label3" runat="server" Text='&lt;%# Bind("startdate", "{0:M/d/yyyy}") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField HeaderText="Expiry Date" SortExpression="endDate"&gt;
                              &lt;EditItemTemplate&gt;
                                  &lt;asp:TextBox ID="TextBox1" runat="server" Text='&lt;%# Bind("enddate", "{0:M/d/yyy}") %&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
                                      TargetControlID="TextBox1" Format="M/d/yyyy"&gt;
                                  &lt;/asp:CalendarExtender&gt;
                              &lt;/EditItemTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="lblEndDate" runat="server" Text='&lt;%# Bind("enddate", "{0:M/d/yyyy}") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                      &lt;/Columns&gt;
                      &lt;EmptyDataTemplate&gt;No valid courses found.&lt;/EmptyDataTemplate&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>UPDATE: I've been trying all your suggestions, but I get exceptions or nulls or empty strings. I've even re-created the problem in a simpler example and still can't figure it out! I'll keep trying though, and I appreciate any new suggestions.</p>