I've used a radgrid with nestedviewtemplate, but placed a user control in the template as below. The user control displays a form with details about the device (about 20 fields, including model, model description as in the Gridrow). It all works as I would like, however, in the user control, I have the ability to update the "Device". When the Device updates, I'd like the grid to rebind to show the update. Is there a way I can rebind the grid from the user control, and have that row expanded after rebind?
<
telerik:RadGrid
ID
=
"RadGrid1"
DataSourceID
=
"SqlDataSource1"
runat
=
"server"
AutoGenerateColumns
=
"False"
OnItemDeleted
=
"RadGrid1_ItemDeleted"
AllowAutomaticDeletes
=
"True"
ShowStatusBar
=
"true"
OnDataBound
=
"RadGrid1_DataBound"
>
<
MasterTableView
DataSourceID
=
"SqlDataSource1"
DataKeyNames
=
"Device_Id, Model"
AllowMultiColumnSorting
=
"True"
GroupLoadMode
=
"Server"
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Device_Id"
HeaderText
=
"Device_Id"
SortExpression
=
"Device_Id"
UniqueName
=
"Device_Id"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Model"
HeaderText
=
"Model"
SortExpression
=
"Model"
UniqueName
=
"Model"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Model_Desc"
HeaderText
=
"Model_Desc"
SortExpression
=
"Model_Desc"
UniqueName
=
"Model_Desc"
>
</
telerik:GridBoundColumn
>
<
telerik:GridButtonColumn
ConfirmText
=
"Delete this device?"
ConfirmDialogType
=
"RadWindow"
ConfirmTitle
=
"Delete"
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
Text
=
"Delete"
UniqueName
=
"DeleteColumn"
>
<
HeaderStyle
Width
=
"50px"
/>
<
ItemStyle
HorizontalAlign
=
"Center"
CssClass
=
"MyImageButton"
Width
=
"50px"
/>
</
telerik:GridButtonColumn
>
</
Columns
>
<
NestedViewSettings
DataSourceID
=
"SqlDataSource2"
>
<
ParentTableRelation
>
<
telerik:GridRelationFields
DetailKeyField
=
"Device_Id"
MasterKeyField
=
"Device_Id"
/>
</
ParentTableRelation
>
</
NestedViewSettings
>
<
NestedViewTemplate
>
<
asp:Panel
ID
=
"NestedViewPanel"
runat
=
"server"
CssClass
=
"viewWrap"
Width
=
"95%"
>
<
div
class
=
"contactWrap"
>
<
fieldset
style
=
"padding: 10px;"
>
<
legend
style
=
"padding: 5px;"
>Detail info for Device: ID: <%#Eval("Device_Id") %> | Model: <%#Eval("Model") %></
legend
>
<
ucUPNG:ucUPNG
ID
=
"ucUPNG"
runat
=
"server"
Key='<%#Eval("Device_Id") %>' />
</
fieldset
>
</
div
>
</
asp:Panel
>
</
NestedViewTemplate
>
</
MasterTableView
>
<
PagerStyle
Mode
=
"NumericPages"
></
PagerStyle
>
<
ClientSettings
AllowDragToGroup
=
"true"
/>
</
telerik:RadGrid
>
10 Answers, 1 is accepted
Depending on what controls/containers you have in the UserControl, you could try using the NamingContainer object to get a reference to the RadGrid control, as suggested in this forum thread.
As for the RadGrid's expand state, you can see how to preserve it in this CodeLibrary article.
All the best,
Tsvetina
the Telerik team

Thank you for your response. I've tried to follow the links that you've sent, and am trying to use:
RadGrid grid = ((GridEditFormItem)((Button)sender).NamingContainer).OwnerTableView.OwnerGrid as RadGrid;
grid.Rebind();
But it's giving me the following error:
Error: Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of type 'ASP.admin_devices_updatedv_ng_ascx' to type 'Telerik.Web.UI.GridEditFormItem'.
My user control code block (this happens on a button click):
public void Update_Device(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(connString);
try
{
string make = rcbMan.Text;
string mod = rtbModel.Text;
SqlCommand cmd = new SqlCommand("Update_Device", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DeviceID", lblDeviceID.Text);
... just a bunch more parameters...
cmd.Parameters.Add(new SqlParameter("@ReturnValue", SqlDbType.VarChar, 255));
cmd.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
cn.Open();
cmd.ExecuteScalar();
string returnvalue = cmd.Parameters["@ReturnValue"].Value.ToString();
if (returnvalue == "1")
{
Label1.Text = "The device " + make + " " + mod + " was successfully updated.";
RadGrid grid = ((GridEditFormItem)((Button) sender).NamingContainer).OwnerTableView.OwnerGrid as RadGrid;
grid.Rebind();
}
}
finally
{
cn.Close();
}
}
Any ideas? Thanks.
Can you check if the Button Parent's NamingContainer is the GridEditFormItem?
Kind regards,
Iana
the Telerik team


<div id="UpdateGrid" runat="server">
<telerik:RadGrid id="RadGrid1" ...... >
.....
<NestedViewTemplate>
... USER CONTROL
</NestedViewTemplate>
</telerik:RadGrid>
</div>
on my user control...
<div id="Reqiured" runat="server">
...form
<asp:Button id="btnUpdate" runat="server" OnClick="Update_Device" />
</div>
I need to rebind the grid from the button onclick. Nothing in the examples has worked. No matter which code I use, from what was provided or in any of the examples, I get "Object reference not set to an instance of an object" or "Unable to cast object of type 'ASP.admin_devices_updatedv_ng_ascx' to type 'Telerik.Web.UI.GridEditFormItem'"
Oh, and I've tried all of these:
RadGrid rg = ((GridEditFormItem)((Button)sender).NamingContainer).OwnerTableView.OwnerGrid as RadGrid;
RadGrid rg = (Page.FindControl("ucUpDev").FindControl("RadGrid1") as RadGrid);
RadGrid rg = ((RadGrid)((btnUpdate.NamingContainer.FindControl("Required").FindControl("UpdateGrid").FindControl("RadGrid1"))));
RadGrid rg = (RadGrid)Page.FindControl("RadGrid1");
RadGrid rg = (Page.FindControl("RadGrid1") as RadGrid);
rg.Rebind();
Find the attached sample and let me know if it helps.
Greetings,
Iana
the Telerik team

NOPE! I appreciate the response, but that does NOT work either! I get the same "object not set to an instance of an object" error. Plus, I don't see how your example even comes close to the situation that I have described. I am using a nestedviewtemplate with a user control, and your example has a template edit form that exists on the same aspx page as the grid. I don't understand why this should be so difficult and why there are no examples of this in the documentation. Once again:
I have a page with a grid as such:
<div id="UpdateGrid" runat="server">
<telerik:RadGrid id="RadGrid1" ...... >
.....
<NestedViewTemplate>
... USER CONTROL
</NestedViewTemplate>
</telerik:RadGrid>
</div>
on my user control...
<div id="Reqiured" runat="server">
...form
<asp:Button id="btnUpdate" runat="server" OnClick="Update_Device" />
</div>
public
void Update_Device(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(connString);
try
{
string make = rcbMan.Text;
string mod = rtbModel.Text;
SqlCommand cmd = new SqlCommand("Update_Device", cn);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@DeviceID", lblDeviceID.Text);
cmd.Parameters.AddWithValue(
"@DC", rcbCat.SelectedValue);
cmd.Parameters.AddWithValue(
"@SC", rcbSubCat.SelectedValue);
cmd.Parameters.AddWithValue(
"@MN", rcbMan.SelectedValue);
cmd.Parameters.AddWithValue(
"@Model", rtbModel.Text);
cmd.Parameters.AddWithValue(
"@ModelDesc", rtbModelDesc.Text);
cmd.Parameters.AddWithValue(
"@Price", rtbPrice.Text);
cmd.Parameters.Add(
new SqlParameter("@ReturnValue", SqlDbType.VarChar, 255));
cmd.Parameters[
"@ReturnValue"].Direction = ParameterDirection.Output;
cn.Open();
cmd.ExecuteScalar();
string returnvalue = cmd.Parameters["@ReturnValue"].Value.ToString();
if (returnvalue == "1")
{
Label1.Text =
"The device " + make + " " + mod + " was successfully updated.";
*** NEED TO UPDATE GRID ON MAIN PAGE HERE ***
}
finally
{
cn.Close();
}
}

Also,
I have tried changing the line from your example:
GridEditFormItem editedItem = btn.NamingContainer
as GridEditFormItem;
to
GridNestedViewItem
editedItem = btn.NamingContainer as GridNestedViewItem;
and that does NOT work either!
I do not think I can use NamingContainer because the grid does not exist on the usercontrol where the button is.
This forum post has been going on for 17 days and I feel like we are going backwards instead of forwards!

Try the following approach to access the grid from Button inside UserControl.
C#:
protected
void
Update_Device(
object
sender, EventArgs e)
{
Button btn=(Button)sender;
GridNestedViewItem nestedItem = (GridNestedViewItem)btn.NamingContainer.NamingContainer;
RadGrid grid = (RadGrid)nestedItem.OwnerTableView.OwnerGrid;
grid.Rebind();
}
Hope this helps,
Princy.
