This is a migrated thread and some comments may be shown as answers.

Total values of one colum based on another

4 Answers 35 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 17 Mar 2014, 01:27 PM
 I have a column named Rate and another Named LaborHours

The rate column can be two values:  REGULAR and OVERTIME

I want the to totals in the footer:  REGULAR 5,  OVERTIME 10

The code below works in my MS DataGrid, cannot figure out how to get it to work under RadGrid

You help is much appreciated!

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim MilesOrHours As String
   MilesOrHours = (DataBinder.Eval(e.Row.DataItem, "Rate"))

   If MilesOrHours = "MI" Then
     Dim HourTotal As Decimal
     HourTotal = (DataBinder.Eval(e.Row.DataItem, "LaborHours"))
     grdMilesTotal = grdMilesTotal + HourTotal
     Session("grandMilesTotal") = Session("grandMilesTotal") + grdMilesTotal

  Else

     Dim HourTotal As Decimal
     HourTotal = (DataBinder.Eval(e.Row.DataItem, "LaborHours"))
     grdHoursTotal = grdHoursTotal + HourTotal
     Session("grandHoursTotal") = Session("grandHoursTotal") + grdHoursTotal

   End If

End If



If e.Row.RowType = DataControlRowType.Footer Then
    Me.txtHrs.Text = Session("grandHourstotal")
    Me.txtMiles.Text = Session("grandMilesTotal")

    Session("grandMilestotal") = "0"
    Session("grandHourstotal") = "0"
End If

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Mar 2014, 04:00 PM
Hello,

Please  try with the below code snippet.

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
 
        Dim dataitem As dynamic = item.DataItem
        'var dataitem = item.DataItem;   // please do casting here
        Dim MilesOrHours As String = dataitem.Rate
 
        If MilesOrHours = "MI" Then
            Dim HourTotal As Decimal = dataitem.LaborHours
        End If
    End If
 
    If TypeOf e.Item Is GridFooterItem Then
        Dim item As GridFooterItem = TryCast(e.Item, GridFooterItem)
        TryCast(item.FindControl("txtHrs"), TextBox).Text = Session("grandHourstotal").ToString()
    End If
End Sub



Thanks,
Jayesh Goyani
0
Mike
Top achievements
Rank 1
answered on 17 Mar 2014, 04:16 PM
Thanks Jayesh,

I think that is going to work, but I am stuck on:

Dynamic when i post the code, for the word Dynamic I get "Type Expected" I am assuming something is not registered properly.

thanks for any assistance!
m
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Mar 2014, 05:13 AM
Hello,

Please check the type of below code snippet.

item.DataItem

Suppose its type is ABC. (Type is based on your assigned datasource)

So, your code must be same as below.

ABC item = item.DataItem as ABC;
 

Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Mar 2014, 05:21 AM
Hello,

Please check the below code for reference.

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    List<Employee> list = new List<Employee>();
    list.Add(new Employee() { ID = 1, Name = "Name1" });
    list.Add(new Employee() { ID = 2, Name = "Name2" });
    list.Add(new Employee() { ID = 3, Name = "Name3" });
    list.Add(new Employee() { ID = 4, Name = "Name4" });
    list.Add(new Employee() { ID = 5, Name = "Name5" });
    RadGrid1.DataSource = list;
}
 
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        Employee dataitem = item.DataItem as Employee;
 
        string strID = dataitem.ID.ToString();
    }
}

......
......
[Serializable]
public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or