Click here to Skip to main content
14,661,076 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using GridView templates for displaying data, Here i have two columns actual, estimate costs. If there actual cost cross the estimate cost(actual cost grater then estimated), I have to change that row color dynamically in GridView.
Posted
Updated 21-Dec-10 1:51am
v2
Comments
TweakBird 21-Dec-10 7:51am
   
Corrected spells & capitalization.

Rate this:
Please Sign up or sign in to vote.

Solution 1

Handle RowDataBound event of the GridView and apply your logic over there.

RowDataBound event will fire at every row which is created while DataBound
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

For Datagrid the event is 'ItemDataBound' and for Gridview the event is 'RowDataBound'.

sample:
protected void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
     Textbox tbx1 = (TextBox)e.Row.FindControl("ActualCostTextbox");
     Textbox tbx2 = (TextBox)e.Row.FindControl("EstimatedCostTextbox");
     if (tbx1 != null & tbx2 != null)
     { 
         // compare, validate the values of tbx1 & tbx2
         // you got tbx1.Text & tbx2.Text value here. 
         // Do any calculation desired.
         // based on it logic, set the CSS of the row.
     }
}
   
Comments
srinivasvempa 21-Dec-10 5:58am
   
tanx but i am not using any controls in girdview i am binding data with itemtemplate evil
Sandeep Mewara 21-Dec-10 6:08am
   
Itemtemplate itself does not show anything! There must/has to be some control in it. Right?
Rate this:
Please Sign up or sign in to vote.

Solution 3

hi try it
protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      Textbox tbx1 = (TextBox)e.Row.FindControl("ActualCostTextbox");
      Textbox tbx2 = (TextBox)e.Row.FindControl("EstimatedCostTextbox");
      if (tbx1 != null&&tbx2 != null)
      {
         // Your code.....
      }
    }
  }
   
Comments
fjdiewornncalwe 21-Dec-10 9:20am
   
Hey There. Please read the answers already given and if your answer is the same, just note that with a vote or a comment on that answer. As you are new here, I won't downvote you for posting the same answer that Sandeep gave above. Cheers. (Good answer, BTW)

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100