Hi GridView Experts!

I have problems with GridView when adding a column using TemplateField.
I fill GridView1 from a stored procedure. It has a Select statement and returns 14 columns,
AutoGenerateColumns="True".
When I add two columns manually to be able to start drag and drop, the drag and drop works OK, but in GridView1_PreRender() and GridView1_RowCreated() I loose values of GridView1.Rows[i].Cells[2].Text and e.Row.Cells[3].Text, these are "". The e.Row.Cells.Count gives the correct 16, but the cells are "". It is strange, that the grid appears correctly with all the values.
My problem is that I need to set colors based on the Status and Date fields in GridView1_PreRender() or somewhere else. Can anybody help me?
Thank you in advance.

<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" OnPreRender="GridView1_PreRender"
     OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowSorting="True" 
     OnRowCreated="GridView1_RowCreated" OnSorted="GridView1_Sorted" 
     EmptyDataText="No Tasks found with te given criteria" 
     AutoGenerateColumns="True">
     <SelectedRowStyle BackColor="#FFC080" />
     <EmptyDataRowStyle ForeColor="Blue" />
     <Columns>
         <asp:TemplateField HeaderText= "Description">
         <ItemTemplate>
            <div onmousedown='return DayPilotCalendar.dragStart(null, 60*30, "<%# Eval("ID") %>", "");'>
            <asp:Label ID="Label1" runat="Server" unselectable='on' Text='<%# Eval("Description") %>'>
            </asp:Label></div>                               
         </ItemTemplate>
         </asp:TemplateField>         <asp:TemplateField HeaderText= "State">
         <ItemTemplate>
            <asp:Label ID="Label2" runat="Server" Text='<%# Eval("State") %>'></asp:Label>
         </ItemTemplate>
         </asp:TemplateField>
     </Columns>                        
</asp:GridView>

private void SetTaskCalendar()
{
   ...

   GridView1.DataSourceID = "ObjectDataSource1";
   GridView1.DataBind();
   ...
}

protected void GridView1_PreRender(object sender, EventArgs e)
{
   for (int i = 0; i < GridView1.Rows.Count; i++)
   {
       //Overdue Tasks (not Ready and not Accepted)
       if ((Convert.ToDateTime(GridView1.Rows[i].Cells[3].Text).Date <= DateTime.Now.Date.Date) &
           (GridView1.Rows[i].Cells[2].Text != "Ready") && (GridView1.Rows[i].Cells[2].Text != "Accepted"))
       {
           GridView1.Rows[i].ForeColor = System.Drawing.Color.Red;
       }
       //Ready Tasks
       if (GridView1.Rows[i].Cells[2].Text == "Ready") GridView1.Rows[i].ForeColor = System.Drawing.Color.Green;
   }
}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    String ID = e.Row.Cells[3].Text;
}

I have got the solution from an other forum.
Get the value of the TemplateFields using FindControl():

protected void GridView1_PreRender(object sender, EventArgs e)
{
   for (int i = 0; i < GridView1.Rows.Count; i++)
   {
      Control TemplateField1 = GridView1.Rows[i].[COLOR="red"]FindControl("Label1");
      Label Label_TF1 = (Label)TemplateField1;[/COLOR]       

       //Overdue Tasks (not Ready and not Accepted)
       if ((Convert.ToDateTime(GridView1.Rows[i].Cells[3].Text).Date <= DateTime.Now.Date.Date) &
           ([COLOR="red"]Label_TF1.Text[/COLOR]!= "Ready") && ([COLOR="red"]Label_TF1.Text[/COLOR]!= "Accepted"))
       {
           GridView1.Rows[i].ForeColor = System.Drawing.Color.Red;
       }
       //Ready Tasks
       if ([COLOR="red"]Label_TF1.Text  [/COLOR]== "Ready") GridView1.Rows[i].ForeColor = System.Drawing.Color.Green;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.