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

RadGrid NestedViewTemplate w UserControl need to rebind grid

10 Answers 302 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 24 Aug 2010, 01:16 AM
Hi,

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

Sort by
0
Tsvetina
Telerik team
answered on 26 Aug 2010, 01:59 PM
Hi Jason,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jason
Top achievements
Rank 1
answered on 27 Aug 2010, 01:26 AM
Tsvetina,

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.

 

 

0
Iana Tsolova
Telerik team
answered on 01 Sep 2010, 09:53 AM
Hello Jason,

Can you check if the Button Parent's NamingContainer is the GridEditFormItem?

Kind regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jason
Top achievements
Rank 1
answered on 07 Sep 2010, 05:51 PM
Can you help a little more?  I'm sorry, but I do not know what you are asking.
0
Jason
Top achievements
Rank 1
answered on 07 Sep 2010, 07:47 PM
I've tried everything and cannot get this to work.  PLEASE help!  This is my scenario:

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

 

0
Iana Tsolova
Telerik team
answered on 09 Sep 2010, 12:51 PM
Hi Jason,

Find the attached sample and let me know if it helps.

Greetings,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jason
Top achievements
Rank 1
answered on 10 Sep 2010, 07:57 PM
Lana,

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

}

}


0
Jason
Top achievements
Rank 1
answered on 10 Sep 2010, 08:07 PM

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!

0
Princy
Top achievements
Rank 2
answered on 13 Sep 2010, 09:02 AM
Hello Jason,

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.
0
Jason
Top achievements
Rank 1
answered on 13 Sep 2010, 05:06 PM
BINGO!  That works.  Thank you Princy!
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Jason
Top achievements
Rank 1
Iana Tsolova
Telerik team
Princy
Top achievements
Rank 2
Share this question
or