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

Please give suggestion to this nested grid problem

3 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
nagendra
Top achievements
Rank 1
nagendra asked on 17 Oct 2010, 10:36 AM
Hi ,
I am new to telerik control. I am using nested radgrid. I need to add new record detail table onclick of link button in master table. Please find the screen shot attached.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Oct 2010, 07:32 AM
Hello Negendra,

Try the following code snippet to achieve your requirement.

ASPX:
<telerik:RadGrid ID="RadGrid1" OnItemCommand="RadGrid1_ItemCommand" 
  OnPreRender="RadGrid1_PreRender">
    <MasterTableView runat="server" DataKeyNames="EmployeeID"
      CommandItemDisplay="Top">
           <Columns>
              <telerik:GridButtonColumn CommandName="AddNewItem" Text="Add New Item">
              </telerik:GridButtonColumn>
           </Columns>
        <DetailTables>
           . . . . . . . . . .
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)//to make the DetailTable in insert mode when clicking the 'AddNewItem' button in MasterTable
   {
       if (e.CommandName == "AddNewItem")
       {
           GridDataItem item = (GridDataItem)e.Item;
           item.Expanded = true;
           GridTableView tableView = (GridTableView)item.ChildItem.NestedTableViews[0];
           tableView.IsItemInserted = true;
           tableView.Rebind();
 
       }
   }
   protected void RadGrid1_PreRender(object sender, EventArgs e)//to make the first row of MasterTable as expanded state.
   {
       RadGrid1.Items[0].Expanded = true;
   }

Thanks,
Princy.
0
nagendra
Top achievements
Rank 1
answered on 18 Oct 2010, 08:10 AM
Thanks for the quick reply Princy.

When I click on "Add New item" in parent row, fields with header will be added in vertical way. I wanted the fields to be in single row so I inserted the code "tableView.EditMode = GridEditMode.InPlace;".
But the problem is I am not getting any "Insert/Cancel" buttons and also if I press enter keyboard button, fields with headers are again switched to vertical look.

One more question, when I display the fields in single row, some fields should be calculated in code behind and marked as read-only fields in grid. Can I achieve this kind in RadGrid control?

Thanks,
Nagendra
0
Accepted
Princy
Top achievements
Rank 2
answered on 19 Oct 2010, 07:03 AM
Hello Negendra,

When using Inplace editmode, you need to have the EditCommandColumn displayed in order to show the insert and cancel buttons. That means you need to add one EditCommandColumn and from code behind hide the Edit links and show Insert/Cancel buttons only.

ASPX:
<DetailTables>
    <telerik:GridTableView EditMode="InPlace" Name="GridTableView1" . . . . . .>
         <Columns>
           <telerik:GridEditCommandColumn UniqueName="EditcolumnUniqueName"></telerik:GridEditCommandColumn>
           <telerik:GridBoundColumn DataField="TerritoryID" UniqueName="TerritoryID"></telerik:GridBoundColumn>
         </Columns>
                . . . . . . . . . .
      </telerik:GridTableView>
 </DetailTables>

C#:
// hiding the Edit links of DetailTable
  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted && e.Item.OwnerTableView.Name == "GridTableView1") // check with name of DetailTable
        {
            GridDataInsertItem insertItem = (GridDataInsertItem)e.Item;
            GridDataItem parentitem = (GridDataItem)insertItem.OwnerTableView.ParentItem;
            GridTableView tableView = (GridTableView)parentitem.ChildItem.NestedTableViews[0];
            tableView.GetColumn("EditcolumnUniqueName").Visible = true;
            foreach (GridDataItem dataItem in tableView.Items)
                   ((LinkButton)dataItem["EditcolumnUniqueName"].Controls[0]).Visible = false;
         }
        else
        {
            foreach (GridDataItem parentitem in RadGrid1.Items)
            {
                if (parentitem.Expanded)
                {
                    GridTableView tableView = (GridTableView)parentitem.ChildItem.NestedTableViews[0];
                    tableView.GetColumn("EditcolumnUniqueName").Visible = false;
                }
            }
         }
    }

The following code snippet shows how to set some default values and mark these fields as RreadOnly.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
     if (e.Item is GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted && e.Item.OwnerTableView.Name == "GridTableView1")
       {
           GridDataInsertItem insertItem = (GridDataInsertItem)e.Item;
           TextBox txt = (TextBox)insertItem["TerritoryID"].Controls[0]; // accessing the control using its UniqueName
           txt.Text = "1234";
           txt.ReadOnly = true;
        }
    }

Thanks,
Princy.
Tags
Grid
Asked by
nagendra
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
nagendra
Top achievements
Rank 1
Share this question
or