Accessing the edited grid item from the user control

4 posts, 0 answers
  1. Joe
    Joe avatar
    6 posts
    Member since:
    Feb 2015

    Posted 31 Mar 2016 Link to this post

    I have a UserControl as custom edit form inside my grid. It's bound directly to a data item using the Data Item property as demonstrated here.


    private object _dataItem = null;
    public object DataItem
          get {return this._dataItem;}
          set {this._dataItem = value;}


    Binding expressions display values in controls like this:

     <asp:TextBox ID="txtApples" runat="server" Text='<%# Bind("Apples") %>' Width="50px" Enabled="false" ></asp:TextBox>
     <asp:TextBox ID="txtOranges" runat="server" Text='<%# Bind("Oranges") %>' Width="50px" ></asp:TextBox>


    Users can edit the number of Oranges and divide Apples/Oranges on button click. The button click event handled in the ascx.cs file, which does the math and updated the value displayed in txtApples.


    Now, I want to add a Reset button to the UserControl. When clicked, the original Apple value will display in txtApples. Seems like this value would be availble in the Edited Grid Item as described here

    Accessing the edited grid item from the user control

    When RadGrid loads the UserControl Edit Form, the UserControl has full access to the properties of the grid item in which it is loaded. Inside theUserControl, you can access the grid item using the Parent.NamingContainer property:

    GridEditableItem editedItem = this.Parent.NamingContainer;

    Once you have a reference to the GridEditableItem object, you can access any of its properties, such as cell text values, the DataItem object (available in DataBinding event handler), and so on.

    How can I access the Apple cell text value?




  2. Joe
    Joe avatar
    6 posts
    Member since:
    Feb 2015

    Posted 01 Apr 2016 in reply to Joe Link to this post

    To simplify my question, how can I assign a Grid cell values to a variable in my ascx.cs page?  
  3. Eyup
    Eyup avatar
    4081 posts

    Posted 04 Apr 2016 Link to this post

    Hi Joe,

    In this case you can cast the item to GridEditFormItem and access the cell of the corresponding row:
    string cellValue = (item as GridEditFormItem).ParentItem["ShipName"].Text;

    You can also use DataKeyNames to achieve this requirement:

    I hope this will prove helpful.

    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  4. Joe
    Joe avatar
    6 posts
    Member since:
    Feb 2015

    Posted 08 Apr 2016 in reply to Eyup Link to this post

    Thanks, Eyup. This worked. 

    protected void Button1_Click(object sender, EventArgs e)
          GridEditableItem editedItem = (GridEditableItem)this.Parent.NamingContainer;
          string sr_string = (editedItem as GridEditFormItem).ParentItem["sample_rate"].Text.ToString();

Back to Top