Due to some requirements the datasource in the nestedviewtemplate are supplied programmatic in the ItemCommand,
everything works fine when expanding and the data are loaded but when I click collapse the selected row won't collapse and instead it just post back and displays the same-thing.
Is there a way to programmatically collapse the nestedview of a selected row when collapse is clicked?
Also I'm not allowed to use sqldatasource because the data is from List<T>.
Thanks!
11 Answers, 1 is accepted
Try using Advanced DataBinding for the grid in NestedViewTemplate. Attach OnNeedDataSource event to grid and write the code for populating grid in the event handler, instead of trying in ItemCommand event. Please checkout the demo which demonstrates how to use Advanced DataBinding.
Advanced Data Binding
Kind regards,
Pavlina
the Telerik team
I've already done that but it has a problem, the data in the nested view wont show up unless I bind the radgrid.
And as we all know we cannot call Bind after the OnNeedDataSource.
(Please Note that inside the nested view are asp:details view that are binded to the source)
Thanks!
If the issue persists, it will be best if you send us a small runnable project which demonstrates the issue. Thus we will be able to we debug the project and provide you with more to-the-point answer.
Greetings,
Pavlina
the Telerik team
Can you make sure that EnableViewState property of the grid is set to true? Please take into account that some operations in Telerik RadGrid like data extraction through the ExtractValuesFromItem method, grouping, hierarchical views expand/collapse, custom edit forms or filtering require that view state is enabled.
All the best,
Pavlina
the Telerik team
private void CollapseAll()
{
foreach (GridItem item in radGridDocument.MasterTableView.Controls[0].Controls)
{
if (item is GridGroupHeaderItem)
{
item.Expanded = false;
}
}
}
private void ExpandAll()
{
foreach (GridItem item in radGridDocument.MasterTableView.Controls[0].Controls)
{
if (item is GridGroupHeaderItem)
{
item.Expanded = true;
}
}
}
This is the code i am using on click event,but even though grid is not getting collapse.
Please follow the approach from the code library below in order to implement the desired functionality and see if it works as expected.
Custom expand/collapse column with ExpandAll/CollapseAll image button in the header
I hope this helps.
All the best,
Pavlina
the Telerik team
I also have the same issue and resolved it.
For resolving the issue , i used two hidden fields
<asp:HiddenField ID="hdnExpandCollapse" Value="0" runat="server" />
<asp:HiddenField ID="hdnExpanded" Value="0" runat="server" />
Then the following two grid events are used to capture the state of the grid item
/* Start functions used for collapse the grid */
protected void Grid_PreRender(object sender, EventArgs e)
{
int i = 0;
foreach (GridDataItem item in Grid.MasterTableView.Items)
{
GridTableView DetailsTable = (GridTableView)item.OwnerTableView;
System.Collections.Hashtable ht = DetailsTable.DataKeyValues[i];
string strDataKey= ht["DataKey"].ToString();
if (strDataKey == hdnExpandCollapse.Value)
{
if (hdnExpanded.Value == Territory)
{
item.Expanded = false;
hdnExpanded.Value = "0";
}
else
{
item.Expanded = true;
hdnExpanded.Value = Territory;
}
}
i++;
}
}
protected void Grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExpandCollapseCommandName)
{
hdnExpandCollapse.Value = ((EntityClass)(e.Item.DataItem)).DataKey.ToString();
}
}
/* End functions used for collapse the grid */
Thank you for sharing your solution with the community.
Regards,
Pavlina
Telerik