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

Suppressing command item in a self-referencing hierarchical radgrid

5 Answers 159 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Casey
Top achievements
Rank 1
Casey asked on 16 Aug 2011, 08:58 PM
I have a self-referencing hierarchical grid set up. I need to have insert functionality available for any hierarchical level. When a row is expanded to show the children records I need to hide the non selected parent records and disable the selected parent. This is working correctly. The issue I need guideance on is that I also need to suppress the option to insert a row into the parent's hierarchical level if a child is open. Otherwise, at each level of hierarchy there is an option to insert a record which is confusing to the users since we are hiding the rows for that hierarchical level. I'm sure looking at the code could make this a little more clear . . .

This is a simple self-refrencing hierarchical grid with automatic inserts enabled:
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Web20" 
    OnItemCreated="RadGrid1_ItemCreated"  
    OnItemDataBound="RadGrid1_ItemDataBound" OnColumnCreated="RadGrid1_ColumnCreated"
    AutoGenerateColumns="False" ShowStatusBar="True"
    CellSpacing="0" GridLines="None" AllowAutomaticInserts="True">
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
    <MasterTableView DataKeyNames="CATEGORY_CDE, PARENT_CDE" ShowHeader="true" 
        HierarchyDefaultExpanded="false" AllowSorting="true" 
        CommandItemDisplay="Bottom" editmode="InPlace" InsertItemDisplay="Bottom">
        <SelfHierarchySettings KeyName="CATEGORY_CDE" ParentKeyName="PARENT_CDE" />
        <CommandItemSettings ExportToPdfText="Export to PDF" 
            AddNewRecordText="Add New Category"></CommandItemSettings>
        <Columns>
            <telerik:GridExpandColumn FilterControlAltText="Filter ExpandColumn column" 
                Visible="True">
            </telerik:GridExpandColumn>
            <telerik:GridEditCommandColumn UniqueName="UpdateColumn" 
                FilterControlAltText="Filter EditCommandColumn column" Visible="True" 
                EditText="" InsertText="" 
                CancelText="" ButtonType="ImageButton" EditImageUrl="../images/blank.gif">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="Category_Cde" UniqueName="Category_Cde" Visible="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Parent_cde" UniqueName="Parent_cde" Visible="true">
            </telerik:GridBoundColumn>
            </Columns>
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" 
                Visible="True">
        <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <EditFormSettings>
        <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
    </MasterTableView>
    <ClientSettings AllowExpandCollapse="true" />
    <FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

Here is the ItemCommand which is suppressing the non selected hierarchical rows. There is a block of noted out code in the middle of this which shows a couple of things I have tried without success.
Protected Sub RadGrid1_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    Dim itemKey As String
    'Inactivates non selected hierarchy levels
    If (e.CommandName = RadGrid.ExpandCollapseCommandName) Then
        Dim currentItem As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim currentRowIndex As Integer = currentItem.ItemIndex
        Dim currentCatKey As String = currentItem.GetDataKeyValue("CATEGORY_CDE").ToString()
        Dim currentParentKey As String = currentItem.GetDataKeyValue("PARENT_CDE").ToString()
        If (Not currentItem.Expanded) Then
            'If an item is expanded then hide the other rows at that level
            For Each ditem As GridDataItem In RadGrid1.Items
                itemKey = ditem.GetDataKeyValue("PARENT_CDE").ToString()
                If currentRowIndex <> ditem.ItemIndex And currentParentKey = ditem.GetDataKeyValue("PARENT_CDE").ToString() Then
                    'Trying to find the right way to suppress the option to add categories for unopened levels
                    'RadGrid1.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = False
                    'RadGrid1.MasterTableView.CommandItemSettings.ShowRefreshButton = False
                    ' RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None
                    'RadGrid1.AllowAutomaticInserts = False

                    'Enables the fields for the row
                    EnableDisableItem(ditem, True)
                           ditem.Visible = False
                Else
                    'Disables the fields for the row
                    EnableDisableItem(ditem, False)
                End If
            Next
        Else
            For Each ditem As GridDataItem In RadGrid1.Items
                itemKey = ditem.GetDataKeyValue("PARENT_CDE").ToString()
                If currentParentKey = ditem.GetDataKeyValue("PARENT_CDE").ToString() Then
                    ditem.Visible = True
                    'Enables fields for row
                    EnableDisableItem(ditem, True)
                End If
            Next
        End If
    End If
    If (e.CommandName = "PerformInsert") Then
        e.Canceled = True
        Dim dgi As GridDataInsertItem = TryCast(e.Item, GridDataInsertItem)
        updateCategory("AddCat", dgi)
        RadGrid1.EditIndexes.Clear()
        RadGrid1.Rebind()
    End If
    rbtnSave.Visible = True
End Sub


Please let me know if there is a way to do this. My next step was going to be to throw out the self-referencing and add detail tables for each level of hierarchy, but that seems down right silly.

Thanks,
Casey

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Aug 2011, 07:13 AM
Hello Casey,

You can hide CommandItemDisplay for parent level in PreRender event as shown below.
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
        GridCommandItem item = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
        item.Display = false;
}

Thanks,
Princy.
0
Casey
Top achievements
Rank 1
answered on 17 Aug 2011, 02:31 PM
Thanks again Princy!

This works for the first level of hierarchy, but how would you suppress subsequent levels?

Example:
1) Hierarchy level 1
    2) Hierarchy level 2
        3) Hierarchy level 3
        3) Add new record displays
    2) Add new record displays <- How would I suppress this?
1) Add new record option is suppressed using your code below

I wish I could attach a screenshot. It would be a lot easier to see the issue.

Casey
0
Tsvetina
Telerik team
answered on 22 Aug 2011, 09:36 AM
Hi Casey,

You can hide the GridCommandItem the same way as the GridHeaderItem is hidden in our online demo on ItemCreated:
Self-referencing Hierarchy

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridCommandItem && e.Item.OwnerTableView != RadGrid1.MasterTableView)
    {
        e.Item.Style["display"] = "none";
    }
}


Greetings,
Tsvetina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Casey
Top achievements
Rank 1
answered on 22 Aug 2011, 03:08 PM
Thanks Tsvetina,

The code provided raises the following error:

Operator '<>' is not defined for types 'Telerik.Web.UI.GridTableView' and Telerik.Web.UI.GridTableView'.

If TypeOf e.Item Is GridCommandItem AndAlso e.Item.OwnerTableView <> RadGrid1.MasterTableView Then
    e.Item.Style("display") = "none"
End If
0
Tsvetina
Telerik team
answered on 22 Aug 2011, 03:54 PM
Hi Casey,

In VB, as the demo, shows you should use:
If TypeOf e.Item Is GridCommandItem AndAlso Not e.Item.OwnerTableView Is RadGrid1.MasterTableView Then
    e.Item.Style("display") = "none"
End If


All the best,
Tsvetina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Casey
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or