Jun
12
Written by:
host
6/12/2008 6:40 PM
Most of the applications which I develop are report driven, time critical information systems. For this reason I have always found it useful to be able to format the output of a datagrid at runtime. In most cases, I will just change the background colour of a cell\row, in other cases I will change the text depending on the criteria.
Merging Two Fields
Firstly you will need this function in your code page.
ProtectedFunction createFullName(ByVal fName AsObject, ByVal sName AsObject) AsString
Return CType(fName, String) & " " & CType(sName, String)
EndFunction
In Order to call this function you need to change the text properties of the label field.
<asp:Label runat="server" Text='<%# createFullName(DataBinder.Eval(Container, "DataItem.Forename"), DataBinder.Eval(Container, "DataItem.Surname")) %>'>
Changing Cell Properties
In this example I am changing the back color depending on whether a target has been met.
Protected Sub gV_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
tempDays = DateDiff(DateInterval.Day, Now(), tempdate)
If e.Row.RowType = DataControlRowType.DataRow Or e.Row.RowType = DataControlRowType.Header Then
If e.Row.Cells(2).Text <> "" Then
tempdate = DateTime.Parse(e.Row.Cells(2).Text)
tempDays = DateDiff(DateInterval.Day, Now(), tempdate)
If tempDays < 0 Then
e.Row.BackColor = Drawing.Color.Red
End If
If tempDays >= 0 And tempDays < 5 Then
e.Row.BackColor = Drawing.Color.Orange
End If
If tempDays > 4 Then
e.Row.BackColor = Drawing.Color.LightGreen
End If
End If
End If
End Sub
Tags: