Copy data from one Control to another RadGrid

5 posts, 2 answers
  1. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 25 Nov 2013 Link to this post

    I need to copy data from one control  --"PlannedStartDate"  in RadGrid using GridTemplateColumns to "bStartDate", but the code below that I am using is not working. Any suggestions?
    <telerik:GridTemplateColumn HeaderText="New Start Date" UniqueName="bStartDate"  >                                                         
      <ItemTemplate>
      <asp:Label runat="server" ID="bStartDate" Text=" ">
      </asp:Label
       </ItemTemplate>
                                                                  
     </telerik:GridTemplateColumn>
     
     
     
     
    <telerik:GridTemplateColumn HeaderText="Planned Start Date" UniqueName="PlannedStartDate" >                                                          
      <ItemTemplate>
      <asp:Label runat="server" ID="plannedstartdate" Text='<%# String.Format("{0:d}" , Eval("PlannedStartDate")) %>'>
     </asp:Label
      </ItemTemplate>
     <EditItemTemplate>
      <br />
     <telerik:RadDatePicker ID="picker3" runat="server" DbSelectedDate='<%# Bind("PlannedStartDate") %>'>
      </telerik:RadDatePicker>
     <asp:CustomValidator ID="CustomValidator3" ControlToValidate="picker3" ErrorMessage="This field was incorrect"
        runat="server" OnServerValidate="CustomValidator1_ServerValidate">
      </asp:CustomValidator>
     </EditItemTemplate>
           </telerik:GridTemplateColumn>
     
     
     
     
     
     
     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
     
     
             
     
                  
                if (e.Item is GridCommandItem && blnApproved)
                {
                    GridCommandItem dataitem = (GridCommandItem)e.Item;
                      
                    Label PStart = dataitem.FindControl("plannedstartdate") as Label;
                                     
                     
                    Label BStart = dataitem.FindControl("bStartDate") as Label;
                     
                    BStart.Text = PStart.ToString();
                     
                }
     
     
            }
     
        }
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Nov 2013 Link to this post

    Hi Ebenezer,

    Please try the following code snippet.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            Label lblPlanStartDate = (Label)item.FindControl("plannedstartdate");
            string valuestartdate = lblPlanStartDate.Text;
            if (blnApproved)
            {
                Label lblbStartDate = (Label)item.FindControl("bStartDate");
                lblbStartDate.Text = valuestartdate;
            }
        }
    }

    Thanks,
    Princy
  3. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 26 Nov 2013 Link to this post

    Hi Princy,
    I have given your code a try. For some reasons this is returning null
    Label lblPlanStartDate = (Label)item.FindControl("plannedstartdate");
    and therefore blowing up at
    string valuestartdate = lblPlanStartDate.Text;
    Infact I have to change the code  to use GridCommandItem   because the your code was not reaching GridDataItem


    if (e.Item is GridCommandItem)
               {
                   GridCommandItem item = (GridCommandItem)e.Item;
                   
                   Label lblPlanStartDate = (Label)item.FindControl("plannedstartdate");
                   string valuestartdate = lblPlanStartDate.ToString();
                   if (blnApproved)
                   {
                       Label lblbStartDate = (Label)item.FindControl("bStartDate");
                       lblbStartDate.Text = valuestartdate;
                   }
               }

  4. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Nov 2013 Link to this post

    Hi Ebenezer,

    The GridTableView object has an Items property that contains all the data rows in the table view. Each row is represented by a GridDataItem or GridEditFormItem object, depending on whether the row is an edit form. Since you are trying to access values of columns, you should access through GridDataItem. We use GridCommandItem, when we want to access controls inside the CommandItem, that is displayed on top or at the bottom of the each GridTableView base on the settings of CommandItemDisplay property. The above code works fine at my end, can you please provide your full code snippet to identify the issue.

    Thanks,
    Princy




  5. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 27 Nov 2013 Link to this post

    Hi Princy
    You are right. It was my mistake. I inserted the code in the wrong place. It works. Thanks
Back to Top