Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using a gridview and i am changing the color of row depending on cell value at RowDataBound event of gridview.
I am using template fields, ItemTemplate and EditItemTemplate , in ItemTemplate i have used Label control and in EditItemTemplate TextBox, now when i load page it doesnt show me any error but when i press edit button on gridview it gives exception like doent find any label control in edit mode,
i tried code as follows-
C#
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
        {
            Label lblSalary = (Label)e.Row.FindControl("lblSalary");
            decimal Sal = Convert.ToDecimal(lblSalary.Text);
            if (Sal > 15000)
            {
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
            }
        }


but its not working properly, please help me how to find ItemTemplate and EditItemTemplate

[edit] wrap code in pre tag[/edit]
Posted
Updated 20-Mar-11 21:38pm
v4
Comments
Venkatesh Mookkan 21-Mar-11 3:33am    
The error means lblSalary does not exists.

Post your ASPX code for further help

1 solution

chang this as

C#
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)        
{           
    Label lblSalary = (Label)e.Row.FindControl("lblSalary");     
    decimal Sal = 0;
    if(lblSalary!=null)
    {
       Sal = Convert.ToDecimal(lblSalary.Text);        
    }
    else
    {
      TextBox txtSalary = (TextBox)e.Row.FindControl("txtSalary");
      Sal=Convert.ToDecimal(txtSalary.Text);
    }
    if (Sal > 15000)          
    {                
      e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;     
    }    
}


or you can use gridview1.editrowindex>=0

--Pankaj
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900