create a template field and calculate age

5 posts, 0 answers
  1. Dave
    Dave avatar
    11 posts
    Member since:
    Dec 2011

    Posted 06 Jul 2012 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 07 Jul 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dave
    Dave avatar
    11 posts
    Member since:
    Dec 2011

    Posted 07 Jul 2012 Link to this post

    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
              }
           }
  5. Dave
    Dave avatar
    11 posts
    Member since:
    Dec 2011

    Posted 08 Jul 2012 Link to this post

    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
  6. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 09 Jul 2012 Link to this post

    Hello,

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


    Thanks,
    Jayesh Goyani
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017