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 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:

    <div>
     <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>
     </div>

     

    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 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. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eyup
    Admin
    Eyup avatar
    3009 posts

    Posted 04 Apr 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:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

    I hope this will prove helpful.

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

    Posted 08 Apr 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