subtract two label dates and use button to insert value in

3 posts, 1 answers
  1. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 19 Jun 2014 Link to this post

    Hi ,

    dears , I just need your help as I've a grid contain two labels they are retrieving dates " start_date " " end_date "
    I already create a "diff_button", I need when I press in this button to subtract the dates and get the difference in months
    and then insert the value in a cell "difference "
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Jun 2014 in reply to Mohamed El-Sayed Link to this post

    Hi Beka,

    I guess you have a template column with labels to display date and on a button click you want to show the difference in month in the third column. Please try the following code snippet:

    ASPX:
    <asp:Button ID="btnDifference" runat="server" Text="Get Difference" OnClick="btnDifference_Click" />
    <telerik:RadGrid ID="RadGrid1" runat="server" . . >
        <MasterTableView >
            <Columns>          
                <telerik:GridTemplateColumn HeaderText="StartDate">
                    <ItemTemplate>
                        <asp:Label ID="lblStartDate" runat="server" Text='<%# String.Format("{0:MM/dd/yy}", Eval("StartDate"))%>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="EndDate">
                    <ItemTemplate>
                        <asp:Label ID="lblEndDate" runat="server" Text='<%# String.Format("{0:MM/dd/yy}", Eval("EndDate"))%>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:Label ID="lblDifference" runat="server"></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void btnDifference_Click(object sender, EventArgs e)
    {
      foreach (GridDataItem dataItem in RadGrid1.Items)
      {
        //Access StartDate label
        Label lblStartDate = (Label)dataItem.FindControl("lblStartDate");
        DateTime startdate = Convert.ToDateTime(lblStartDate.Text);
        //Access EndDate label
        Label lblEndDate = (Label)dataItem.FindControl("lblEndDate");
        DateTime endDate =Convert.ToDateTime(lblEndDate.Text);   
        //Get the difference
        int difference = ((endDate.Year - startdate.Year) * 12) + endDate.Month - startdate.Month;
        //Access label to display the result
        Label lblDifference = (Label)dataItem.FindControl("lblDifference");
        lblDifference.Text = difference.ToString();
      }
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 21 Jun 2014 in reply to Princy Link to this post

    Thanks Princy ;)
Back to Top