Conditional formatting of nested gridview cells

  • Thread starter Thread starter Nitz
  • Start date Start date
N

Nitz

Hi,

How can i change the forecolor/backcolor of a cell in a nested gridview
based on the content of the cell ?

It's easy to do it with the master gridview with the rowdatabound event but
i have no clue how to do it with nested controls.
 
Hi Nitz

It's just an extension of the rowdatabound event. I'm assuming that your
nested grid is using a value from the out grid as a parameter for stored
procedure or something similar. My code is below

Protected Sub gvRange_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRange.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim s As SqlDataSource =
CType(e.Row.FindControl("dsRangeInner"), SqlDataSource)
s.SelectParameters(2).DefaultValue = e.Row.Cells(1).Text
counter += 1
Dim gvOrders As GridView = CType(e.Row.FindControl("gvOrders"),
GridView)
gvOrders.DataBind()
Dim row As GridViewRow
For Each row In gvOrders.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim chkAvailable As CheckBox =
CType(row.FindControl,"chkAvailable"), CheckBox)
Dim chkPlanned As CheckBox =
CType(row.FindControl("chkPlanned"), CheckBox)
If Not (chkPlanned.Checked Or chkOilAvailable.Checked)
Then
row.Cells(0).ForeColor = Drawing.Color.Red
End If

End If

Next

End If
End Sub

For each row in the outer grid, I pick up the gridview in the template, make
sure I do databind on the inner gridview then step through the rows in the
same way as on the outer gridview, using the values of a couple of checkboxes
to decide the format of my order number column. I hope I've explained that
well enough, it sounds a bit garbled to me, but should be easy enough to
follow.
 
Back
Top