CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Create a Grid Column with a Calculated Value

This example shows how to create a column with a calculated value in a Grid. In this case, we have a Grid called Tasks which has the fields task_name, estimated_time and expended_time. We shall add a new field called task_percent which will contain the calculated value.

  1. Add an additional column to the grid. There are two ways to do this:
  2. Click the Label icon on the Forms tab (of the Toolbox ) to insert a new Label control into the Grid column.
  3. Type task_percent into the Name property.
  4. Select float in the Data Type property.
  5. Enter 0.00 or any other value into the Format property.
  6. Add the Before Show Row event to the Grid.
  7. Inside the event, add the code below:

ASP

Function Tasks_BeforeShowRow(Sender) 

  If Tasks.Estimated_Time.Value > 0 Then
    Tasks.Task_Percent.Value = (Tasks.Expended_Time.Value*100)/Tasks.Estimated_Time.Value
  Else
    Tasks.Task_Percent.Value = 0
  End if 
   
End Function

PHP

function Tasks_BeforeShowRow(& $sender) {
global$Tasks; 
 
  if($Tasks->Estimated_Time->GetValue() > 0){
    $Tasks->Task_Percent->SetValue( ($Tasks->Expended_Time->GetValue()*100)/$Tasks->Estimated_Time->GetValue()); 
  } else{
    $Tasks->Task_Percent->SetValue(0);
  }

}

Perl

sub Tasks_BeforeShowRow() { 

  if ($Tasks->{Estimated_Time}->GetValue() > 0) {
     $Tasks->{Task_Percent}->SetValue( ($Tasks->{Expended_Time}->GetValue()*100)/$Tasks->{Estimated_Time}->GetValue());
  } else {
     $Tasks->{Task_Percent}->SetValue(0);
  }

}

ColdFusion

<!---Tasks_BeforeShowRow ---> 
  <CFIF fldEstimated_Time GT 0>
    <CFSET fldTask_Percent=(fldExpended_Time*100)/fldEstimated_Time>
  <CFELSE>
    <CFSET fldTask_Percent=0>
  </CFIF>

VB.Net

'Tasks_BeforeShowRow
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim TasksTask_Percent As System.Web.UI.WebControls.Literal = DirectCast(e.Item.FindControl("TasksTask_Percent"),System.Web.UI.WebControls.Literal)
    If DataItem.Estimated_Time.Value > 0 Then
      TasksTask_Percent.Text = ((DataItem.Expended_Time.Value*100)/DataItem.Estimated_Time.Value).ToString()
    Else
      TasksTask_Percent.Text = "0"
    End if 
  End If

C#

//Tasks_BeforeShowRow

  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
    System.Web.UI.WebControls.Literal TasksTask_Percent = (System.Web.UI.WebControls.Literal)e.Item.FindControl("TasksTask_Percent");
    if ((Double)DataItem.Estimated_Time.Value > 0) {
      TasksTask_Percent.Text = (((Double)DataItem.Expended_Time.Value*100)/(Double)DataItem.Estimated_Time.Value).ToString();
    } else {
      TasksTask_Percent.Text = "0";
    } 
  }

Java

//Tasks_BeforeShowRow

  if(e.getGrid().getControl("Estimated_Time").getValue() != null && 
      Utils.convertToDouble(e.getGrid().getControl("Estimated_Time").getValue()).doubleValue()>0 ){
    e.getGrid().getControl("Task_Percent").setValue( 
      (Utils.convertToDouble(e.getGrid().getControl("Expended_Time").getValue()).doubleValue()*100)/Utils.convertToDouble(e.getGrid().getControl("Estimated_Time").getValue()).doubleValue()); 
  } else {
    e.getGrid().getControl("Task_Percent").setValue(0);
  }

See also:

Before Show Row event


On-line, printable versions and updates