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

How to expand the detailtable after the row updated in the masterview?

2 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Will
Top achievements
Rank 1
Will asked on 12 Aug 2013, 03:48 AM
Basically I have a MasterTableView / DetailTable relationship. In the master table,the rows are editable.After I updated the row,the rows were collpased.I want the detailtable expanded which related to the updated row.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 12:10 PM
Hi Will,

Please try the below code snippet to keep the rows expanded even after updating.

ASPX:
<radG:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" Skin="WebBlue" EnableAJAX="true" EnableAJAXLoadingTemplate="true" AutoGenerateEditColumn="true"  AutoGenerateColumns="False" AllowSorting="True" AllowMultiRowSelection="true" AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AutoGenerateDeleteColumn="true" AllowAutomaticUpdates="true"    OnDataBound="RadGrid1_DataBound" OnItemCommand="RadGrid1_ItemCommand">
    <PagerStyle Mode="NumericPages"></PagerStyle>
    <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" Width="100%"
        CommandItemDisplay="Top">
        <DetailTables>
            <radG:GridTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource2" Width="100%"
                runat="server" CommandItemDisplay="Top" PageSize="10">
                <ParentTableRelation>
                    <radG:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                </ParentTableRelation>
                <Columns>
                    <radG:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton"
                        DataField="OrderID" UniqueName="OrderID">
                    </radG:GridBoundColumn>
                    <radG:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton"
                        DataField="OrderDate" UniqueName="OrderDate">
                    </radG:GridBoundColumn>
                    <radG:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton"
                        DataField="EmployeeID" UniqueName="EmployeeID">
                    </radG:GridBoundColumn>
                </Columns>
            </radG:GridTableView>
        </DetailTables>
        <Columns>
            <radG:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton"
                DataField="CustomerID" UniqueName="CustomerID">
            </radG:GridBoundColumn>
            <radG:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton"
                DataField="ContactName" UniqueName="ContactName">
            </radG:GridBoundColumn>
            <radG:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton"
                DataField="CompanyName" UniqueName="CompanyName">
            </radG:GridBoundColumn>
        </Columns>
    </MasterTableView>
</radG:RadGrid>

C#:
private Hashtable _ordersExpandedState;
private Hashtable _selectedState;
 
public void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //reset expanded states
        this._ordersExpandedState = null;
        this.Session["_ordersExpandedState"] = null;
    }
}
//Save/load expanded states Hash from the session
//this can also be implemented in the ViewState
private Hashtable ExpandedStates
{
    get
    {
        if (this._ordersExpandedState == null)
        {
            _ordersExpandedState = this.Session["_ordersExpandedState"] as Hashtable;
            if (_ordersExpandedState == null)
            {
                _ordersExpandedState = new Hashtable();
                this.Session["_ordersExpandedState"] = _ordersExpandedState;
            }
        }
 
        return this._ordersExpandedState;
    }
}
 
//Clear the state for all expanded children if a parent item is collapsed
private void ClearExpandedChildren(string parentHierarchicalIndex)
{
    string[] indexes = new string[this.ExpandedStates.Keys.Count];
    this.ExpandedStates.Keys.CopyTo(indexes, 0);
    foreach (string index in indexes)
    {
        //all indexes of child items
        if (index.StartsWith(parentHierarchicalIndex + "_") ||
            index.StartsWith(parentHierarchicalIndex + ":"))
        {
            this.ExpandedStates.Remove(index);
        }
    }
}
//Save/load selected states Hash from the session
//this can also be implemented in the ViewState
private Hashtable SelectedStates
{
    get
    {
        if (this._selectedState == null)
        {
            _selectedState = this.Session["_selectedState"] as Hashtable;
            if (_selectedState == null)
            {
                _selectedState = new Hashtable();
                this.Session["_selectedState"] = _selectedState;
            }
        }
 
        return this._selectedState;
    }
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    //save the expanded/selected state in the session
    if (e.CommandName == RadGrid.ExpandCollapseCommandName)
    {
        //Is the item about to be expanded or collapsed
        if (!e.Item.Expanded)
        {
            //Save its unique index among all the items in the hierarchy
            this.ExpandedStates[e.Item.ItemIndexHierarchical] = true;
        }
        else //collapsed
        {
            this.ExpandedStates.Remove(e.Item.ItemIndexHierarchical);
            this.ClearExpandedChildren(e.Item.ItemIndexHierarchical);
        }
    }
    //Is the item about to be selected
    else if (e.CommandName == RadGrid.SelectCommandName)
    {
        //Save its unique index among all the items in the hierarchy
        this.SelectedStates[e.Item.ItemIndexHierarchical] = true;
    }
    //Is the item about to be deselected
    else if (e.CommandName == RadGrid.DeselectCommandName)
    {
        this.SelectedStates.Remove(e.Item.ItemIndexHierarchical);
    }
}
protected void RadGrid1_DataBound(object sender, EventArgs e)
{
    //Expand all items using our custom storage
    string[] indexes = new string[this.ExpandedStates.Keys.Count];
    this.ExpandedStates.Keys.CopyTo(indexes, 0);
 
    ArrayList arr = new ArrayList(indexes);
    //Sort so we can guarantee that a parent item is expanded before any of
    //its children
    arr.Sort();
 
    foreach (string key in arr)
    {
        bool value = (bool)this.ExpandedStates[key];
        if (value)
        {
            RadGrid1.Items[key].Expanded = true;
        }
    }
 
    //Select all items using our custom storage
    indexes = new string[this.SelectedStates.Keys.Count];
    this.SelectedStates.Keys.CopyTo(indexes, 0);
 
    arr = new ArrayList(indexes);
    //Sort to ensure that a parent item is selected before any of its children
    arr.Sort();
 
    foreach (string key in arr)
    {
        bool value = (bool)this.SelectedStates[key];
        if (value)
        {
            RadGrid1.Items[key].Selected = true;
        }
    }
}
protected void grdRebind_Click(object sender, EventArgs e)
{
    RadGrid1.Rebind();
}

Thanks,
Princy
0
Will
Top achievements
Rank 1
answered on 28 Oct 2013, 09:43 AM
Thank you very much Princy.
Tags
Grid
Asked by
Will
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Will
Top achievements
Rank 1
Share this question
or