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

Grid - Parent / Child editing

4 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Contact
Top achievements
Rank 1
Contact asked on 19 Apr 2012, 04:01 PM
Hi

I have a fairly standard radgrid with masterview and detail tables.

When editing an item at the top level (parent item), how would I go about auto-expanding the child rows and putting the parent and child rows in edit mode?

Thanks
David

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 20 Apr 2012, 07:52 AM
Hi David,

I guess you want the parent as well as child item in edit mode when you put a particular row in edit.Try the following code snippet.

C#:
public static Int32 EmpId = 0;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem eitem = (GridEditableItem)e.Item;
        Int32 Id = Convert.ToInt32((eitem["EmployeeID"].Controls[0] as TextBox).Text); //EmployeeID is the DataKeyName
        EmpId = Id;
    
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        GridTableView tableView = (GridTableView)item.ChildItem.NestedTableViews[0];
        if (Convert.ToInt32(item.GetDataKeyValue("EmployeeID").ToString()) == EmpId)
        {          
            item.Expanded = true; //Expanding parent row
            foreach (GridItem childItem in tableView.Items)
            {  
                 GridEditableItem editableItem = childItem as GridDataItem;
                 editableItem.Edit = true//making child item editable
            }
        }
        tableView.Rebind();
    }
}

Regards,
Princy.
0
Contact
Top achievements
Rank 1
answered on 24 Apr 2012, 03:54 PM
Thanks, the code you provided worked.

This led to another issue, once the child elements are expanded I can't then save or cancel - due to the following error:

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

The viewstate is enabled on the radgrid.

Do you have any suggestions?  Would it be possible to make changes to both the parent and child rows then perform a single save?

Thanks
0
Princy
Top achievements
Rank 1
answered on 25 Apr 2012, 12:52 PM
Hi David,

Unfortunately I couldn't replicate the error you are facing.Here is the complete code I tried with a  modification in the PreRender.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
    DataSourceID="SqlDataSource1" OnItemDataBound="RadGrid1_ItemDataBound"
    OnPreRender="RadGrid1_PreRender" AllowMultiRowEdit="true"
    onitemcommand="RadGrid1_ItemCommand">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID" Name="Master">
        <Columns>
            <telerik:GridBoundColumn DataField="EmployeeID" UniqueName="EmployeeID" HeaderText="EmployeeID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
        </Columns>
        <DetailTables>
            <telerik:GridTableView DataKeyNames="TerritoryID" Name="Detail" DataSourceID="SqlDataSource2">
                <Columns>
                    <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="TerritoryID" HeaderText="TerritoryID" UniqueName="TerritoryID">
                    </telerik:GridBoundColumn>
                        <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                </Columns>
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="EmployeeID" MasterKeyField="EmployeeID" />
                </ParentTableRelation>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

C#:
public static Int32 EmpId = 0;
public static string commandName = "";
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem eitem = (GridEditableItem)e.Item;
        Int32 Id = Convert.ToInt32((eitem["EmployeeID"].Controls[0] as TextBox).Text); //EmployeeID is the DataKeyName
        EmpId = Id;
    }
}
 
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView.Name == "Master")
    {
        commandName = "Edit";
    }
}
 
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (commandName == "Edit")
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            GridTableView tableView = (GridTableView)item.ChildItem.NestedTableViews[0];
            if (Convert.ToInt32(item.GetDataKeyValue("EmployeeID").ToString()) == EmpId)
            {
                item.Expanded = true; //Expanding parent row
                foreach (GridItem childItem in tableView.Items)
                {
                    GridEditableItem editableItem = childItem as GridDataItem;
                    editableItem.Edit = true//making child item editable
                }
            }
            tableView.Rebind();
 
        }
        commandName = "";
    }
}

Thanks,
Princy.
0
Anuja
Top achievements
Rank 1
answered on 13 Jun 2014, 04:59 AM
Hi ,



I want grid as per the attached image. In telerik grid am not able to find any thing related to that. Please do support me for better
development and progress.



Thanks and Regards

Anuja.J
Tags
Grid
Asked by
Contact
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Contact
Top achievements
Rank 1
Anuja
Top achievements
Rank 1
Share this question
or