This is a migrated thread and some comments may be shown as answers.

Accessing the edited grid item from the user control

3 Answers 473 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 31 Mar 2016, 08:11 PM

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?

 

 

 

3 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 1
answered on 01 Apr 2016, 06:01 PM
To simplify my question, how can I assign a Grid cell values to a variable in my ascx.cs page?  
0
Eyup
Telerik team
answered on 04 Apr 2016, 05:46 AM
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.
0
Joe
Top achievements
Rank 1
answered on 08 Apr 2016, 05:28 PM

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();
                
    }

Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Joe
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or