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

Self-referencing hierarchy and HierarchyDefaultExpanded issue

7 Answers 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nikola
Top achievements
Rank 1
Nikola asked on 04 Nov 2010, 09:36 AM
Hi!

I have a self-referencing grid with custom CRUD operations (NeedDataSource and custom OnItemCommand events). It is made after this demo http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/selfreferencing/defaultcs.aspx and this article http://www.telerik.com/help/aspnet-ajax/grdselfreferencinghierarchy.html.

When i set the HierarchyDefaultExpanded to true, everything works fine, but I have a lot of data in my grid so I would like to collapse everything at the start. So, when I set the HierarchyDefaultExpanded to false, the grid is initially collapsed, but the edit form doesn't show on child items. The edit work only on the top level of the hierarchy.

This is my grid
<telerik:RadGrid ID="RG_MENU_MenuItem" runat="server"
                     DataSourceID="ods_MENU_MenuItem"
                     AutoGenerateColumns="false"
                     AllowSorting="false"
                     OnNeedDataSource="RG_MENU_MenuItem_NeedDataSource"
                     OnColumnCreated="RG_MENU_MenuItem_ColumnCreated"
                     OnItemDataBound="RG_MENU_MenuItem_ItemDataBound"
                     OnItemCreated="RG_MENU_MenuItem_ItemCreated"
                     OnItemCommand="RG_MENU_MenuItem_ItemCommand"
                     OnInsertCommand="RG_MENU_MenuItem_InsertCommand"
                     OnUpdateCommand="RG_MENU_MenuItem_UpdateCommand"
                     OnDeleteCommand="RG_MENU_MenuItem_DeleteCommand">
        <MasterTableView HierarchyDefaultExpanded="true"
                         HierarchyLoadMode="Client"
                         AllowSorting="false"
                         EditMode="PopUp"
                         DataKeyNames="MenuItemID, ParentItemID"
                         Width="100%">
            <SelfHierarchySettings ParentKeyName="ParentItemID" KeyName="MenuItemID" />
            <Columns>      
                <telerik:GridBoundColumn DataField="MenuItemID"
                                         HeaderText="ID"
                                         SortExpression="MenuItemID"
                                         UniqueName="MenuItemID"
                                         DataType="System.Int64"
                                         AllowFiltering="false"
                                         ReadOnly="true">
                    <HeaderStyle CssClass="rgHeader column0"/>
                    <ItemStyle CssClass="column0" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AppDescription"
                                         HeaderText="App Description"
                                         SortExpression="AppDescription"
                                         UniqueName="AppDescription"
                                         CurrentFilterFunction="Contains"
                                         AutoPostBackOnFilter="true">
                    <HeaderStyle CssClass="rgHeader column1"/>
                    <ItemStyle CssClass="column1" />
                </telerik:GridBoundColumn>              
                <telerik:GridTemplateColumn DataField="MenuInstanceID"
                                            HeaderText="Menu Instance"
                                            SortExpression="MenuInstanceID"
                                            UniqueName="MenuInstanceID">
                                            <FilterTemplate>
                                                <telerik:RadComboBox ID="rcbMenuInstanceFilter" runat="server"
                                                                     DataSourceID="ods_MenuInstance"
                                                                     DataTextField="AppDescription"
                                                                     DataValueField="MenuInstanceID"
                                                                     AllowCustomText="true"
                                                                     AppendDataBoundItems="true"
                                                                     Width="100"
                                                                     SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("MenuInstanceID").CurrentFilterValue %>'
                                                                     OnClientSelectedIndexChanged="rcbMenuInstanceFilter_SelectedIndexChanged">
                                                    <Items>
                                                        <telerik:RadComboBoxItem Text="" Value="" />
                                                    </Items>
                                                </telerik:RadComboBox>
                                                <telerik:RadScriptBlock ID="rsbMenuInstanceFilter" runat="server">
                                                    <script type="text/javascript">
                                                        function rcbMenuInstanceFilter_SelectedIndexChanged(sender, args) {
                                                            var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                                            tableView.filter("MenuInstanceID", args.get_item().get_value(), "EqualTo");
                                                        }
                                                    </script>
                                                </telerik:RadScriptBlock>
                                            </FilterTemplate>
                                            <ItemTemplate>
                                                <%# Eval("MenuInstance.AppDescription")%>
                                            </ItemTemplate>
                    <HeaderStyle CssClass="rgHeader column2"/>
                    <ItemStyle CssClass="column2" />
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn DataField="PageID"
                                            HeaderText="Page"
                                            SortExpression="PageID"
                                            UniqueName="PageID">
                                            <FilterTemplate>
                                                <telerik:RadComboBox ID="rcbPageFilter" runat="server"
                                                                     DataSourceID="ods_Page"
                                                                     DataTextField="AppDescription"
                                                                     DataValueField="PageID"
                                                                     AllowCustomText="true"
                                                                     AppendDataBoundItems="true"
                                                                     Width="100"
                                                                     SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("PageID").CurrentFilterValue %>'
                                                                     OnClientSelectedIndexChanged="rcbPageFilter_SelectedIndexChanged">
                                                    <Items>
                                                        <telerik:RadComboBoxItem Text="" Value="" />
                                                    </Items>
                                                </telerik:RadComboBox>
                                                <telerik:RadScriptBlock ID="rsbPageFilter" runat="server">
                                                    <script type="text/javascript">
                                                        function rcbPageFilter_SelectedIndexChanged(sender, args) {
                                                            var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                                            tableView.filter("PageID", args.get_item().get_value(), "EqualTo");
                                                        }
                                                    </script>
                                                </telerik:RadScriptBlock>
                                            </FilterTemplate>
                                            <ItemTemplate>
                                                <%# Eval("Page.AppDescription")%>
                                            </ItemTemplate>
                    <HeaderStyle CssClass="rgHeader column3"/>
                    <ItemStyle CssClass="column3" />
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="WebOrder"
                                         HeaderText="Web Order"
                                         SortExpression="WebOrder"
                                         UniqueName="WebOrder"
                                         DataType="System.Int32"
                                         FilterControlWidth="25px">
                    <HeaderStyle CssClass="rgHeader column5"/>
                    <ItemStyle CssClass="column5" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="IsActive"
                                         HeaderText="Is Active"
                                         SortExpression="IsActive"
                                         UniqueName="IsActive"
                                         DataType="System.Boolean">
                    <HeaderStyle CssClass="rgHeader column6"/>
                    <ItemStyle CssClass="column6" />
                </telerik:GridBoundColumn>         
                <telerik:GridBoundColumn DataField="TimeStamp"
                                         DataType="System.DateTime"
                                         HeaderText="Time Stamp"
                                         SortExpression="TimeStamp"
                                         UniqueName="TimeStamp"
                                         AllowFiltering="false">
                    <HeaderStyle CssClass="rgHeader column10"/>
                    <ItemStyle CssClass="column10" />
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn CommandName="CustomEdit"
                                          ButtonType="ImageButton"
                                          UniqueName="EditColumn"
                                          ImageUrl="~/_Med_App/images/edit.gif">
                    <HeaderStyle CssClass="rgHeader column11"/>
                    <ItemStyle CssClass="column11" />
                </telerik:GridButtonColumn>
                <telerik:GridButtonColumn ConfirmText="Jeste li sigurni da želite obrisati odabranu stavku?"
                                          CommandName="Delete"
                                          ButtonType="ImageButton"
                                          UniqueName="DeleteColumn">
                    <HeaderStyle CssClass="rgHeader column12"/>
                    <ItemStyle CssClass="column12" />
                </telerik:GridButtonColumn>
                <telerik:GridTemplateColumn UniqueName="SelectColumn"
                                            AllowFiltering="true"
                                            Display="false">
                                            <FilterTemplate>
                                                <asp:CheckBox ID="cbxSelectAllForSync" runat="server" AutoPostBack="true"
                                                              OnCheckedChanged="SelectAll_CheckedChanged" />                                               
                                            </FilterTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="cbxSelectForSync" runat="server" />
                                            </ItemTemplate>
                    <HeaderStyle CssClass="rgHeader column13"/>
                    <ItemStyle CssClass="column13" />
                </telerik:GridTemplateColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <EditColumn UniqueName="EditCommandColumn1"></EditColumn>
                <FormTemplate>
                    <table class="edit-table-rad" cellspacing="2" cellpadding="1" width="100%" border="0">
                        <tr>
                            <td align="left" width="200px">
                                <b>MenuItemID:</b>
                            </td>
                            <td>
                                <%# Eval("MenuItemID").ToString()%>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>ParentItem:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadComboBox ID="rcbParentItemID" runat="server"
                                                     DataSourceID="ods_MENU_ParentMenuItem"
                                                     DataTextField="AppDescription"
                                                     DataValueField="MenuItemID"
                                                     AppendDataBoundItems="true"
                                                     AllowCustomText="true"
                                                     Filter="StartsWith"
                                                     Width="350px">
                                    <Items>
                                        <telerik:RadComboBoxItem Text="" Value="" />
                                    </Items>
                                </telerik:RadComboBox>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Menu Instance:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadComboBox ID="rcbMenuInstanceID" runat="server"
                                                     DataSourceID="ods_MenuInstance"
                                                     DataTextField="AppDescription"
                                                     DataValueField="MenuInstanceID"
                                                     AllowCustomText="true"
                                                     Width="350px">
                                </telerik:RadComboBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfvMenuInstanceID" runat="server"
                                                            ControlToValidate="rcbMenuInstanceID"
                                                            ErrorMessage="Obavezan unos!"
                                                            ForeColor="Red">
                                </asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Page:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadComboBox ID="rcbPageID" runat="server"
                                                     DataSourceID="ods_Page"
                                                     DataTextField="AppDescription"
                                                     DataValueField="PageID"
                                                     AppendDataBoundItems="True"
                                                     AllowCustomText="true"
                                                     Width="350px">
                                    <Items>
                                        <telerik:RadComboBoxItem Text="" Value="" />
                                    </Items>
                                </telerik:RadComboBox>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Menu Item Url:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadTextBox ID="txtMenuItemUrl" runat="server" Width="350px"/>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>App Description:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadTextBox ID="txtAppDescription" runat="server" Width="350px"/>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Web Order:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadNumericTextBox ID="rnbWebOrder" runat="server" NumberFormat-DecimalDigits="0" Width="350px"/>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfvWebOrder" runat="server"
                                                            ControlToValidate="rnbWebOrder"
                                                            ErrorMessage="Obavezan unos!"
                                                            ForeColor="Red">
                                </asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Is Active:</b>
                            </td>
                            <td width="350px">
                                <asp:CheckBox ID="chbIsActive" runat="server" Checked="false"/>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" width="200px">
                                <b>Priority Factor:</b>
                            </td>
                            <td width="350px">
                                <telerik:RadNumericTextBox ID="rnbPriorityFactor" runat="server" NumberFormat-DecimalDigits="0" Width="350px"/>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfvPriorityFactor" runat="server"
                                                            ControlToValidate="rnbPriorityFactor"
                                                            ErrorMessage="Obavezan unos!"
                                                            ForeColor="Red">
                                </asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="InsertUpdateButton" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
                                <asp:Button ID="CancelButton" runat="server" Text="Cancel" CausesValidation="false" CommandName="Cancel"></asp:Button>
                            </td>
                        </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings AllowExpandCollapse="true" />
    </telerik:RadGrid>

The code behind is: (omitted some code that is not relevant)
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using admin_bl;
using admin_bo;
using Telerik.Web.UI;
 
namespace admin
{
    public partial class MENU_MenuItem_Hierarchy : bl_BasePage
    {
        #region "Properties"
 
        private string _strAppMode = string.Empty;
        private Hashtable _htOdsValues;
 
        private bool _isRowFound = false;
        private long _lMenuInstanceIDFilter;
 
        #endregion
 
        #region "Page Load Methods"
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
 
            SetGridDisplay();
 
            SetInitialFilter();
        }
 
        public void Page_PreRenderComplete(object sender, EventArgs e)
        {
            HideExpandColumnRecursive(this.RG_MENU_MenuItem.MasterTableView);
        }
 
        /// <summary>
        /// Changes the grid display based on the ApplicationMode
        /// </summary>
        private void SetGridDisplay()
        {
            this._strAppMode = (Convert.ToInt32(bl_ApplicationData.AppMode)).ToString();
            this.txtAppMode.Text = this._strAppMode;
 
            if (bl_ApplicationData.AppMode == bl_ApplicationData.ApplicationMode.SYNCRONIZATION)
            {
                //disable insert, update & delete
                this.RG_MENU_MenuItem.AllowAutomaticInserts = false;
                this.RG_MENU_MenuItem.AllowAutomaticUpdates = false;
                this.RG_MENU_MenuItem.AllowAutomaticDeletes = false;
                //hide edit & delete columns and show select column
                this.RG_MENU_MenuItem.MasterTableView.GetColumn("EditColumn").Display = false;
                this.RG_MENU_MenuItem.MasterTableView.GetColumn("DeleteColumn").Display = false;
                this.RG_MENU_MenuItem.MasterTableView.GetColumn("SelectColumn").Display = true;
                //show buttons
                this.bDoSyncUP.Visible = true;
                this.bDoSyncDOWN.Visible = true;
                // confirm message
                this.bDoSyncUP.Attributes.Add("OnClick", "return confirm('Jeste li sigurni da želite pokrenuti sinkronizaciju? ');");
                this.bDoSyncDOWN.Attributes.Add("OnClick", "return confirm('Jeste li sigurni da želite pokrenuti sinkronizaciju? ');");
                //set sync legend colors and show it
                this.WUC_sync_legend1.bckColorInsert = bl_ApplicationData.BckColorInsert;
                this.WUC_sync_legend1.bckColorUpdate = bl_ApplicationData.BckColorUpdate;
                this.WUC_sync_legend1.bckColorDelete = bl_ApplicationData.BckColorDelete;
                this.WUC_sync_legend1.isSyncMode = true;
            }
            else if (bl_ApplicationData.AppMode == bl_ApplicationData.ApplicationMode.ADMINISTRATION)
            {
                this.RG_MENU_MenuItem.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
            }
        }
 
        private void SetInitialFilter()
        {
            this.RG_MENU_MenuItem.MasterTableView.FilterExpression = "(it.ParentItemID == 0)";
            //glavni menu initial filter
            this.rcbMenuInstanceFilter.SelectedValue = "8";
            SetMenuInstanceFilter("8");
        }
 
        #endregion
 
        #region "RadGrid Events"
 
        protected void RG_MENU_MenuItem_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            GetDataSource();
        }
 
        protected void RG_MENU_MenuItem_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridExpandColumn)
            {
                e.Column.Visible = false;
            }
            else if (e.Column is GridBoundColumn)
            {
                e.Column.HeaderStyle.Width = Unit.Pixel(100);
            }
        }
 
        protected void RG_MENU_MenuItem_ItemDataBound(object sender, GridItemEventArgs e)
        {
            CreateExpandCollapseButton(e.Item, "MenuItemID");
        }
 
        protected void RG_MENU_MenuItem_ItemCreated(object sender, GridItemEventArgs e)
        {
            CreateExpandCollapseButton(e.Item, "MenuItemID");
 
            //e.Item.Expanded = false;
 
            if (e.Item is GridHeaderItem && e.Item.OwnerTableView != this.RG_MENU_MenuItem.MasterTableView)
            {
                e.Item.Style["display"] = "none";               
            }
 
            if (e.Item is GridNestedViewItem)
            {
                e.Item.Cells[0].Visible = false;               
            }           
 
            if (bl_ApplicationData.AppMode == bl_ApplicationData.ApplicationMode.ADMINISTRATION)
            {
                this.ItemCreatedAdmin(e);
            }
            else if (bl_ApplicationData.AppMode == bl_ApplicationData.ApplicationMode.SYNCRONIZATION)
            {
                this.ItemCreatedSync(e);
            }
        }
 
        protected void RG_MENU_MenuItem_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "CustomEdit")
            {
                EditRow(e);
            }
        }
 
        protected void RG_MENU_MenuItem_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GetValues(e);
 
            Insert();
        }
 
        protected void RG_MENU_MenuItem_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GetValues(e);
 
            Update(e);
        }
 
        protected void RG_MENU_MenuItem_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            Delete(e);
        }
 
        public void SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            this.RG_MENU_MenuItem.Rebind();
 
            foreach (GridDataItem item in this.RG_MENU_MenuItem.MasterTableView.Items)
            {
                (item.FindControl("cbxSelectForSync") as CheckBox).Checked = (sender as CheckBox).Checked;
            }
        }
 
        #endregion
 
        #region "Insert - Update - Delete"
 
        private void Insert()
        {
            string strMessage = String.Empty;
            bool isInserted = false;
 
            new bl_MENU_MenuItem().Insert(Convert.ToInt64(this._htOdsValues["ParentItemID"])
                                        , Convert.ToInt64(this._htOdsValues["MenuInstanceID"])
                                        , Convert.ToInt64(this._htOdsValues["PageID"])
                                        , this._htOdsValues["MenuItemUrl"].ToString()
                                        , this._htOdsValues["AppDescription"].ToString()
                                        , Convert.ToInt32(this._htOdsValues["WebOrder"])
                                        , Convert.ToBoolean(this._htOdsValues["IsActive"])
                                        , base.LoggedUser.UserID
                                        , Convert.ToInt32(this._htOdsValues["PriorityFactor"])
                                        , out isInserted
                                        , out strMessage);
 
            DisplayMessage(!isInserted, strMessage);
        }
 
        private void Update(GridCommandEventArgs e)
        {
            string strMessage = String.Empty;
            bool isUpdated = false;
 
            long lMenuItemID = Convert.ToInt64((e.Item as GridEditableItem).GetDataKeyValue("MenuItemID"));
 
            new bl_MENU_MenuItem().Update(lMenuItemID
                                        , Convert.ToInt64(this._htOdsValues["ParentItemID"])
                                        , Convert.ToInt64(this._htOdsValues["MenuInstanceID"])
                                        , Convert.ToInt64(this._htOdsValues["PageID"])
                                        , this._htOdsValues["MenuItemUrl"].ToString()
                                        , this._htOdsValues["AppDescription"].ToString()
                                        , Convert.ToInt32(this._htOdsValues["WebOrder"])
                                        , Convert.ToBoolean(this._htOdsValues["IsActive"])
                                        , base.LoggedUser.UserID
                                        , Convert.ToInt32(this._htOdsValues["PriorityFactor"])
                                        , out isUpdated
                                        , out strMessage);
 
            DisplayMessage(!isUpdated, strMessage);
        }
 
        private void Delete(GridCommandEventArgs e)
        {
            string strMessage = String.Empty;
            bool isDeleted = false;
 
            long lMenuItemID = Convert.ToInt64((e.Item as GridEditableItem).GetDataKeyValue("MenuItemID"));
 
            new bl_MENU_MenuItem().Delete(lMenuItemID
                                        , out isDeleted
                                        , out strMessage);
 
            DisplayMessage(!isDeleted, strMessage);
        }
 
        #endregion
 
        #region "ObjectDataSource Events"
 
        protected void ods_MENU_MenuItem_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            SetOdsValues(e);
        }
 
        protected void ods_MENU_MenuItem_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            SetOdsValues(e);
        }
 
        protected void ods_MENU_MenuItem_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
 
        }
 
        protected void ods_MENU_MenuItem_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            DisplayMessage(!Convert.ToBoolean(e.OutputParameters["pIsInserted"]), Convert.ToString(e.OutputParameters["pMessage"]));
        }
 
        protected void ods_MENU_MenuItem_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            DisplayMessage(!Convert.ToBoolean(e.OutputParameters["pIsUpdated"]), Convert.ToString(e.OutputParameters["pMessage"]));
        }
 
        protected void ods_MENU_MenuItem_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            DisplayMessage(!Convert.ToBoolean(e.OutputParameters["pIsDeleted"]), Convert.ToString(e.OutputParameters["pMessage"]));
        }
 
        #endregion
 
        #region "Other Events"
 
        protected void button_Click(object sender, EventArgs e)
        {
            ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand";
        }
 
        protected void rcbMenuInstanceFilter_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SetMenuInstanceFilter(e.Value);
        }
 
        #endregion
 
        #region "Utility Methods"
 
        public void GetDataSource()
        {
            RG_MENU_MenuItem.DataSource = new bl_MENU_MenuItem().GetAll( this.txtSortExpression.Text
                                                                       , Convert.ToBoolean(this.txtSortDirection.Text)
                                                                       , this.txtAppMode.Text);
        }
 
        public void HideExpandColumnRecursive(GridTableView pTableView)
        {
            GridItem[] nestedViewItems = pTableView.GetItems(GridItemType.NestedView);
            foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
            {
                foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
                {
                    nestedView.Style["border"] = "0";
 
                    Button btnExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("btnExpandCollapseButton");
                    if (nestedView.Items.Count == 0)
                    {
                        if (btnExpandCollapseButton != null)
                        {
                            btnExpandCollapseButton.Style["visibility"] = "hidden";
                        }
                        nestedViewItem.Visible = false;
                    }
                    else
                    {
                        if (btnExpandCollapseButton != null)
                        {
                            btnExpandCollapseButton.Style.Remove("visibility");
                        }
                    }
 
                    if (nestedView.HasDetailTables)
                    {
                        HideExpandColumnRecursive(nestedView);
                    }
                }
            }
        }
 
        public void CreateExpandCollapseButton(GridItem pItem, string pColumnUniqueName)
        {
            if (pItem is GridDataItem)
            {
                if (pItem.FindControl("btnExpandCollapseButton") == null)
                {
                    Button button = new Button();
                    button.Click += new EventHandler(button_Click);
                    button.CommandName = "ExpandCollapse";
                    button.CssClass = (pItem.Expanded) ? "rgCollapse" : "rgExpand";
                    button.ID = "btnExpandCollapseButton";
 
                    if (pItem.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client)
                    {
                        string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", pItem.Parent.Parent.ClientID);
 
                        button.OnClientClick = script;
                    }
 
                    int level = pItem.ItemIndexHierarchical.Split(':').Length - 1;
 
                    button.Style["margin-left"] = level * 15 + "px";
 
                    TableCell cell = ((GridDataItem)pItem)[pColumnUniqueName];
                    cell.Controls.Add(button);
                    cell.Controls.Add(new LiteralControl(" "));
                    cell.Controls.Add(new LiteralControl(((GridDataItem)pItem).GetDataKeyValue(pColumnUniqueName).ToString()));
                }
            }
        }
 
        private void DisplayMessage(bool pIsError, string pText)
        {
            if (pIsError == true)
            {
                this.lblActionNotOk.Visible = true;
                this.lblActionNotOk.Text = pText;
                this.lblActionOk.Visible = false;
            }
            else
            {
                this.lblActionOk.Visible = true;
                this.lblActionOk.Text = pText;
                this.lblActionNotOk.Visible = false;
            }
        }
 
        private void ItemCreatedAdmin(GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
                geiEditedItem.Visible = true;
 
                if (e.Item.DataItem is bo_MENU_MenuItem)
                {
                    bo_MENU_MenuItem boCurrent = (bo_MENU_MenuItem)e.Item.DataItem;
 
                    RadComboBox rcbParentItemID = geiEditedItem.FindControl("rcbParentItemID") as RadComboBox;
                    rcbParentItemID.SelectedValue = boCurrent.ParentItemID != 0 ? boCurrent.ParentItemID.ToString() : "";
 
                    RadComboBox rcbMenuInstanceID = geiEditedItem.FindControl("rcbMenuInstanceID") as RadComboBox;
                    rcbMenuInstanceID.SelectedValue = boCurrent.MenuInstance.MenuInstanceID.ToString();
 
                    RadComboBox rcbPageID = geiEditedItem.FindControl("rcbPageID") as RadComboBox;
                    rcbPageID.SelectedValue = boCurrent.Page.PageID != 0 ? boCurrent.Page.PageID.ToString() : String.Empty;
 
                    RadTextBox txtMenuItemUrl = geiEditedItem.FindControl("txtMenuItemUrl") as RadTextBox;
                    txtMenuItemUrl.Text = boCurrent.MenuItemUrl.ToString();
 
                    RadTextBox txtAppDescription = geiEditedItem.FindControl("txtAppDescription") as RadTextBox;
                    txtAppDescription.Text = boCurrent.AppDescription.ToString();
 
                    RadNumericTextBox rnbWebOrder = geiEditedItem.FindControl("rnbWebOrder") as RadNumericTextBox;
                    rnbWebOrder.Text = boCurrent.WebOrder.ToString();
 
                    CheckBox chbIsActive = geiEditedItem.FindControl("chbIsActive") as CheckBox;
                    chbIsActive.Checked = boCurrent.IsActive;
 
                    RadNumericTextBox rnbPriorityFactor = geiEditedItem.FindControl("rnbPriorityFactor") as RadNumericTextBox;
                    rnbPriorityFactor.Text = boCurrent.PriorityFactor.ToString();
                }
            }
        }
 
        private void ItemCreatedSync(GridItemEventArgs e)
        {
             
        }
 
        private void GetValues(GridCommandEventArgs e)
        {
            this._htOdsValues = new Hashtable();
            this._htOdsValues.Add("ParentItemID", Convert.ToInt64(((e.Item as GridEditableItem).FindControl("rcbParentItemID") as RadComboBox).SelectedValue != String.Empty ? ((e.Item as GridEditableItem).FindControl("rcbParentItemID") as RadComboBox).SelectedValue : "0"));
            this._htOdsValues.Add("MenuInstanceID", Convert.ToInt64(((e.Item as GridEditableItem).FindControl("rcbMenuInstanceID") as RadComboBox).SelectedValue));
            this._htOdsValues.Add("PageID", Convert.ToInt64(((e.Item as GridEditableItem).FindControl("rcbPageID") as RadComboBox).SelectedValue != String.Empty ? ((e.Item as GridEditableItem).FindControl("rcbPageID") as RadComboBox).SelectedValue : "0"));
            this._htOdsValues.Add("MenuItemUrl", ((e.Item as GridEditableItem).FindControl("txtMenuItemUrl") as RadTextBox).Text);
            this._htOdsValues.Add("AppDescription", ((e.Item as GridEditableItem).FindControl("txtAppDescription") as RadTextBox).Text);
            this._htOdsValues.Add("WebOrder", Convert.ToInt32(((e.Item as GridEditableItem).FindControl("rnbWebOrder") as RadNumericTextBox).Text));
            this._htOdsValues.Add("IsActive", ((e.Item as GridEditableItem).FindControl("chbIsActive") as CheckBox).Checked);
            this._htOdsValues.Add("PriorityFactor", Convert.ToInt32(((e.Item as GridEditableItem).FindControl("rnbPriorityFactor") as RadNumericTextBox).Text));
        }
 
        private void SetOdsValues(ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters["ParentItemID"] = this._htOdsValues["ParentItemID"];
            e.InputParameters["MenuInstanceID"] = this._htOdsValues["MenuInstanceID"];
            e.InputParameters["PageID"] = this._htOdsValues["PageID"];
            e.InputParameters["MenuItemUrl"] = this._htOdsValues["MenuItemUrl"];
            e.InputParameters["AppDescription"] = this._htOdsValues["AppDescription"];
            e.InputParameters["WebOrder"] = this._htOdsValues["WebOrder"];
            e.InputParameters["IsActive"] = this._htOdsValues["IsActive"];
            e.InputParameters["UserID"] = base.LoggedUser.UserID;
            e.InputParameters["PriorityFactor"] = this._htOdsValues["PriorityFactor"];
        }
 
        private void EditRow(GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem gdiItem = (GridDataItem)e.Item;
                long lMenuItemID = Convert.ToInt64(gdiItem.GetDataKeyValue("MenuItemID"));
                this._isRowFound = false;
 
                if (this.RG_MENU_MenuItem.MasterTableView.FindItemByKeyValue("MenuItemID", lMenuItemID) != null)
                    this.RG_MENU_MenuItem.MasterTableView.FindItemByKeyValue("MenuItemID", lMenuItemID).Edit = true;
                else
                    EditRowRecursive(this.RG_MENU_MenuItem.MasterTableView, lMenuItemID);
 
                this.RG_MENU_MenuItem.Rebind();
            }
        }
 
        private void EditRowRecursive(GridTableView pTableView, long pMenuItemID)
        {
            GridItem[] nestedViewItems = pTableView.GetItems(GridItemType.NestedView);
            foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
            {
                if (this._isRowFound)
                    return;
 
                foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
                {
                    if (nestedView.FindItemByKeyValue("MenuItemID", pMenuItemID) != null)
                    {
                        nestedView.FindItemByKeyValue("MenuItemID", pMenuItemID).Edit = true;
                        this._isRowFound = true;
                        return;
                    }
 
                    if (nestedView.HasDetailTables)
                    {
                        EditRowRecursive(nestedView, pMenuItemID);
                    }
                }               
            }
        }
 
        private void SetMenuInstanceFilter(string pMenuInstanceID)
        {
            this._lMenuInstanceIDFilter = Convert.ToInt64(pMenuInstanceID.Equals(String.Empty) ? "0" : pMenuInstanceID);
 
            this.locTitle.Text = "UreÄ‘ivanje Stavki Menua";
            this.RG_MENU_MenuItem.MasterTableView.FilterExpression = "(it.ParentItemID == 0)";
 
            this.txtMenuInstanceID.Text = this._lMenuInstanceIDFilter.ToString();
 
            GridColumn gdcMenuInstanceID = this.RG_MENU_MenuItem.MasterTableView.GetColumnSafe("MenuInstanceID");
 
            if (this._lMenuInstanceIDFilter == 0)
            {
                gdcMenuInstanceID.CurrentFilterFunction = GridKnownFunction.NoFilter;
                gdcMenuInstanceID.CurrentFilterValue = String.Empty;
            }
            else
            {
                this.locTitle.Text += " za MenuInstanceID = " + this.txtMenuInstanceID.Text;
                this.RG_MENU_MenuItem.MasterTableView.FilterExpression += " AND (it.MenuInstanceID == " + this.txtMenuInstanceID.Text + ")";
 
                gdcMenuInstanceID.CurrentFilterFunction = GridKnownFunction.EqualTo;
                gdcMenuInstanceID.CurrentFilterValue = this.txtMenuInstanceID.Text;
            }
 
            this.RG_MENU_MenuItem.MasterTableView.Rebind();
        }
 
        #endregion
 
        #endregion
    }
}

Please help me to have my self-referencing grid initially collapsed and with working edit forms.

7 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 09 Nov 2010, 04:16 PM
Hello Nikola,

As our online documentation states, self-referencing RadGrid hierarchy does not support CRUD operations when it is bound with DataSource control. It may be working in some cases, but not consistently, as it happens in your case.

Regards,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nikola
Top achievements
Rank 1
answered on 10 Nov 2010, 08:21 AM
Hi Tsvetina!

Yes, i read that, but it also states that you can use the NeedDataSource event and custom CRUD operations. I don't use a DataSource object or control, but a bind a List<> to the RadGrid.DataSource property. Is there another method to bind the data to the RadGrid?

Can I maybe use HierarchyDefaultExpanded="true"  and then collapse all the rows later (in some event like ItemDataBound or ItemCreated)?

Regards,
Nikola
0
Tsvetina
Telerik team
answered on 11 Nov 2010, 10:57 AM
Hi Nikola,

Could you please then remove the DataSourceID setting from your RadGrid declaration because it is probably confusing the grid's binding process and see what happens then:
<telerik:RadGrid ID="RG_MENU_MenuItem" runat="server"
DataSourceID="ods_MENU_MenuItem" />


Best wishes,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nikola
Top achievements
Rank 1
answered on 11 Nov 2010, 11:33 AM
Hi Tsvetina,

I removed the DataSourceID from the RadGrid, but it didn't solve the problem. It made some new problems with Eval() but I solved them by removing the GridTemplateColumns.

When everything worked again, without the DataSourceID="ods_MENU_MenuItem" and with the HierarchyDefaultExpanded="false", the edit form still didn't work for non-top-level items!

Any other suggestions or should I try something else, like RadTreeView or something?

Regards,
Nikola
0
Tsvetina
Telerik team
answered on 12 Nov 2010, 10:24 AM
Hi Nikola,

One more thing that could have some impact on the hierarchy loading - could you please check if setting HierarchyLoadMode to ServerOnDemand or ServerBind would make changes to the behavior?

I suppose you meant the RadTreeList control. It is a lot easier to display self-referencing data with it but in the current release it does not yet support CRUD operations, so presently, it may not be an approapriate replacement.

Regards,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nikola
Top achievements
Rank 1
answered on 12 Nov 2010, 01:45 PM
Hi Tsvetina,

I tried all the combination of HierarchyDefaultExpanded and HierarchyLoadMode, and it isn't working.  HierarchyLoadMode="ServerOnDemand"  won't event show the child items, and HierarchyLoadMode="ServerBind" only makes expanding slower. But the edit form won't show in either way.

Yes, I meant the RadTreeList control. But if you say that it also doesn't support CRUD operations, what is the recommended control to display a self-referencing grid with enabled CRUD operations?

Regards,
Nikola
0
Tsvetina
Telerik team
answered on 18 Nov 2010, 09:23 AM
Hi Nikola,

Could you please run this application and try to replicate the problemmatic behavior which you experience in it and then share what changes you made to its source code? Without being able to debug the code, it would be hard to see where exactly the edit form problem comes from if it is not one of the already ruled out reasons.

Regards,
Tsvetina
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Nikola
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Nikola
Top achievements
Rank 1
Share this question
or