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

create a template field and calculate age

4 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 06 Jul 2012, 10:58 PM
Guys I have a field called emp_DOB

I want to add a template field and show the age, how can I do this? I am using the Rad grid

Matt

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Jul 2012, 06:37 AM
Hello Dave,

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            Label label1 = item.FindControl("label1") as Label;
            // Using Data Key
            DateTime dt =Convert.ToDateTime(item.GetDataKeyValue("ShipDate").ToString());
            label1.Text = Convert.ToInt32((DateTime.Now.Subtract(Convert.ToDateTime(item.GetDataKeyValue("ShipDate").ToString())).Days / 365)).ToString(); // this logic is not perfect -- this is only for reference
            //OR
            // Using DataItem / Assigned Datasource
            DataRowView dr = item.DataItem as DataRowView;
            label1.Text = Convert.ToInt32((DateTime.Now.Subtract(Convert.ToDateTime(dr["ShipDate"].ToString())).Days / 365)).ToString(); // this logic is not perfect -- this is only for reference
        }
    }
 
 
 protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Shipper", typeof(string));
        dt.Columns.Add("ShipDate", typeof(DateTime));
        dt.Rows.Add("Shipper1", DateTime.Now.AddYears(-1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddYears(-2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddYears(-3));
        dt.Rows.Add("Shipper1", DateTime.Now.AddYears(-1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddYears(-2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddYears(-3));
 
        RadGrid2.DataSource = dt;
    }
<MasterTableView DataKeyNames="ShipDate">
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="Age">
                        <ItemTemplate>
                            <asp:Label ID="label1" runat="server"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>


Thanks,
Jayesh Goyani
0
Dave
Top achievements
Rank 1
answered on 08 Jul 2012, 12:29 AM
I am real sorry I do not understand this? 

Can someone please break it down a bit more.


I added a template field for AGE I gave it a label called label115

<telerik:GridTemplateColumn FilterControlAltText="Filter OfficerAge column"
            HeaderText="Age" UniqueName="OfficerAge">
            <ItemTemplate>
                            <asp:Label ID="label115" runat="server"></asp:Label>
                        </ItemTemplate>
        </telerik:GridTemplateColumn>


I then added the following piece of code to the cs file....

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem item = e.Item as GridDataItem;
               Label label1 = item.FindControl("label115") as Label;
               // Using Data Key
               DateTime dt = Convert.ToDateTime(item.GetDataKeyValue("emp_DOB").ToString());
               label1.Text = Convert.ToInt32((DateTime.Now.Subtract(Convert.ToDateTime(item.GetDataKeyValue("emp_DOB").ToString())).Days / 365)).ToString(); // this logic is not perfect -- this is only for reference
          }
       }
0
Dave
Top achievements
Rank 1
answered on 09 Jul 2012, 02:58 AM
I am really stumped on this and could reallty use some help.

I just need the Template field OfficerAge to = the age of the emp_DOB officer?

What am I doing wrong, please first grader teacher style and show me please.

Matt
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jul 2012, 05:51 AM
Hello,

Please add below code.
<MasterTableView DataKeyNames="emp_DOB">


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