My problem is
I use a self referencing grid. Im binding a List which has 2 objects. The 1st object has a property called ID set to 1 and ParentID set to 0. The second object has ParentID set to ID for FIrst Object(1). Now when i try to bind the list as datasource to the grid in Page Load,
I get the 1st record displayed and 2nd record as child to first record. The problem is in addition to that, i recieve a duplicate second record in the same level as 1st record.
Any help is appreciated.
I have attached a output which shows the wrong value. There the 2nd object is duplicated once in child view and in the same hierarchy as 1st node.
Following is the code
ASPX
| <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Organization.aspx.cs" Inherits="ConcordanceSample.Organization" %> |
| <%@ Register TagPrefix="telerik" Namespace="Telerik.Charting" Assembly="Telerik.Web.UI" %> |
| <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" > |
| <head id="Head1" runat="server"> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <telerik:RadScriptManager runat="server" ID="ScriptManager1"> |
| </telerik:RadScriptManager> |
| <!-- content start --> |
| <div> |
| <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> |
| <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> |
| <AjaxSettings> |
| <telerik:AjaxSetting AjaxControlID="RadGrid1"> |
| <UpdatedControls> |
| <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> |
| </UpdatedControls> |
| </telerik:AjaxSetting> |
| </AjaxSettings> |
| </telerik:RadAjaxManager> |
| <telerik:RadGrid ID="RadGrid1" runat="server" |
| OnColumnCreated="RadGrid1_ColumnCreated" OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound"> |
| <MasterTableView HierarchyDefaultExpanded="false" HierarchyLoadMode="Client" AllowSorting="true" |
| DataKeyNames="ParentID,ID" Width="100%"> |
| <SelfHierarchySettings ParentKeyName="ParentID" KeyName="ID" /> |
| </MasterTableView> |
| <ClientSettings AllowExpandCollapse="true" /> |
| </telerik:RadGrid> |
| </div> |
| </form> |
| </body> |
| </html> |
CODE - BEHIND
| using System; |
| using System.Collections; |
| using System.Configuration; |
| using System.Data; |
| using System.Linq; |
| using System.Web; |
| using System.Web.Security; |
| using System.Web.UI; |
| using System.Web.UI.HtmlControls; |
| using System.Web.UI.WebControls; |
| using System.Web.UI.WebControls.WebParts; |
| using System.Xml.Linq; |
| using System.Collections.Generic; |
| using Telerik.Web.UI; |
| namespace ConcordanceSample |
| { |
| public partial class Organization : System.Web.UI.Page |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| if (!IsPostBack) |
| { |
| RadGrid1.DataSource = AddMatter(); |
| RadGrid1.DataBind(); |
| } |
| } |
| protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
| { |
| } |
| 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.Items.Count == 0) |
| { |
| 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, "ID"); |
| 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, "ID"); |
| } |
| protected void RadGrid1_PreRender(object sender, EventArgs e) |
| { |
| if (!Page.IsPostBack) |
| { |
| } |
| } |
| 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"; |
| } |
| public List<MatterObject> AddMatter() |
| { |
| List<MatterObject> olistMatter = new List<MatterObject>(); |
| MatterObject objMatter = new MatterObject(); |
| objMatter.ID = 1; |
| objMatter.Name = "Parket & Parker"; |
| objMatter.CreatedBy = "Krishnan"; |
| objMatter.CreationDate = "2/17/2010"; |
| objMatter.LastModifiedDate = "2/17/2010"; |
| objMatter.LastModifiedBy = "Krishnan"; |
| objMatter.ParentID = 0; |
| olistMatter.Add(objMatter); |
| MatterObject objMatter1 = new MatterObject(); |
| objMatter1.ID = 2; |
| objMatter1.Name = "Lancer & Lancer"; |
| objMatter1.CreatedBy = "L.N.K"; |
| objMatter1.CreationDate = "2/17/2010"; |
| objMatter1.LastModifiedDate = "2/17/2010"; |
| objMatter1.LastModifiedBy = "Krishnan"; |
| objMatter1.ParentID = 1; |
| olistMatter.Add(objMatter1); |
| return olistMatter; |
| } |
| public List<MatterObject> AddChild() |
| { |
| List<MatterObject> olistMatter = new List<MatterObject>(); |
| MatterObject objMatter = new MatterObject(); |
| objMatter.ID = 2; |
| objMatter.Name = "Organization Folder 1"; |
| objMatter.CreatedBy = "LN"; |
| objMatter.CreationDate = "2/16/2010"; |
| objMatter.LastModifiedDate = "2/16/2010"; |
| objMatter.LastModifiedBy = "LN"; |
| olistMatter.Add(objMatter); |
| objMatter = new MatterObject(); |
| objMatter.ID = 3; |
| objMatter.Name = "Organization Folder 2"; |
| objMatter.CreatedBy = "Krishnan"; |
| objMatter.CreationDate = "2/15/2010"; |
| objMatter.LastModifiedDate = "2/15/2010"; |
| objMatter.LastModifiedBy = "Krishnan"; |
| olistMatter.Add(objMatter); |
| return olistMatter; |
| } |
| } |
| public class MatterObject |
| { |
| public int ID { get; set; } |
| public string Name { get; set; } |
| public string CreatedBy { get; set; } |
| public string CreationDate { get; set; } |
| public string LastModifiedDate { get; set; } |
| public string LastModifiedBy { get; set; } |
| public int ParentID { get; set; } |
| //public MatterChild Child { get; set; } |
| } |
| } |