Hi All,
I try to make unlimited hierarchical radgrid with SelfHierarchySettings. But when i try to expand a node i can not see the child level inside "No child records to display." I don't want to use HierarchyLoadMode ="Client" so can any body check and inform me what's the problem with my code
Regards ..
I try to make unlimited hierarchical radgrid with SelfHierarchySettings. But when i try to expand a node i can not see the child level inside "No child records to display." I don't want to use HierarchyLoadMode ="Client" so can any body check and inform me what's the problem with my code
Regards ..
| <body> |
| <form id="form1" runat="server"> |
| <asp:ScriptManager ID="sm" runat="server" /> |
| <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnColumnCreated="RadGrid1_ColumnCreated" |
| OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound" |
| OnDetailTableDataBind="RadGrid1_DetailTableDataBind"> |
| <MasterTableView AllowSorting="true" DataKeyNames="FullPath,ParentPath,IsFolder" |
| Width="100%" > |
| <SelfHierarchySettings ParentKeyName="FullPath" KeyName="ParentPath" /> |
| </MasterTableView> |
| <ClientSettings AllowExpandCollapse="true" /> |
| </telerik:RadGrid> |
| </form> |
| </body> |
| public partial class test2 : System.Web.UI.Page |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| if (!Page.IsPostBack) |
| { |
| GridBoundColumn col = new GridBoundColumn(); |
| col.HeaderText = "FullPath"; |
| col.DataField = "FullPath"; |
| RadGrid1.Columns.Add(col); |
| col = new GridBoundColumn(); |
| col.HeaderText = "ParentPath"; |
| col.DataField = "ParentPath"; |
| RadGrid1.Columns.Add(col); |
| RadGrid1.DataSource = GetData("/"); |
| } |
| } |
| private DataTable GetData(string ParentPath) |
| { |
| DataTable dt = new DataTable(); |
| dt.Columns.Add(new DataColumn("FullPath")); |
| dt.Columns.Add(new DataColumn("ParentPath")); |
| dt.Columns.Add(new DataColumn("IsFolder", typeof(bool))); |
| DataRow dr; |
| for (int i = 0; i < 3; i++) |
| { |
| dr = dt.NewRow(); |
| dr[0] = ParentPath + "Folder" + i; |
| dr[1] = ParentPath; |
| dr[2] = true; |
| dt.Rows.Add(dr); |
| } |
| for (int i = 0; i < 2; i++) |
| { |
| dr = dt.NewRow(); |
| dr[0] = ParentPath + "File" + i.ToString() + ".txt"; |
| dr[1] = ParentPath; |
| dr[2] = false; |
| dt.Rows.Add(dr); |
| } |
| return dt; |
| } |
| protected void RadGrid1_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); |
| } |
| } |
| public void Page_PreRenderComplete(object sender, EventArgs e) |
| { |
| HideExpandColumnRecursive(RadGrid1.MasterTableView); |
| } |
| public void HideExpandColumnRecursive(GridTableView tableView) |
| { |
| GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); |
| foreach (GridNestedViewItem nestedViewItem in nestedViewItems) |
| { |
| foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) |
| { |
| nestedView.Style["border"] = "0"; |
| Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton"); |
| if (nestedView.ParentItem.GetDataKeyValue("IsFolder").ToString() == "False") |
| { |
| if (MyExpandCollapseButton != null) |
| { |
| MyExpandCollapseButton.Style["visibility"] = "hidden"; |
| } |
| nestedViewItem.Visible = false; |
| } |
| else |
| { |
| if (MyExpandCollapseButton != null) |
| { |
| MyExpandCollapseButton.Style.Remove("visibility"); |
| } |
| } |
| if (nestedView.HasDetailTables) |
| { |
| HideExpandColumnRecursive(nestedView); |
| } |
| } |
| } |
| } |
| protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
| { |
| CreateExpandCollapseButton(e.Item, "FullPath"); |
| if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView) |
| { |
| e.Item.Style["display"] = "none"; |
| } |
| if (e.Item is GridNestedViewItem) |
| { |
| e.Item.Cells[0].Visible = false; |
| } |
| } |
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| CreateExpandCollapseButton(e.Item, "FullPath"); |
| } |
| public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) |
| { |
| if (item is GridDataItem) |
| { |
| if (item.FindControl("MyExpandCollapseButton") == null) |
| { |
| Button button = new Button(); |
| button.Click += new EventHandler(button_Click); |
| button.CommandName = "ExpandCollapse"; |
| button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand"; |
| button.ID = "MyExpandCollapseButton"; |
| if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) |
| { |
| string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID); |
| button.OnClientClick = script; |
| } |
| int level = item.ItemIndexHierarchical.Split(':').Length; |
| if (level > 1) |
| button.Style["margin-left"] = level + 10 + "px"; |
| TableCell cell = ((GridDataItem)item)[columnUniqueName]; |
| cell.Controls.Add(button); |
| cell.Controls.Add(new LiteralControl(" ")); |
| cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString())); |
| } |
| } |
| } |
| void button_Click(object sender, EventArgs e) |
| { |
| ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"; |
| } |
| protected void RadGrid1_DetailTableDataBind(Object source, GridDetailTableDataBindEventArgs e) |
| { |
| GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem; |
| if (dataItem.GetDataKeyValue("IsFolder").ToString() == "True") |
| { |
| e.DetailTableView.DataSource = GetData(dataItem.GetDataKeyValue("FullPath").ToString()); |
| } |
| } |
| } |