2012-01-20 11 views
0

私のasp.netページにはツリービューがあります。C#asp.netのナビゲーションバー

HierarchicalDataBoundControl only accepts data sources that implement IHierarchicalDataSource or IHierarchicalEnumerable:私は上記の添付の写真のように私はそれをしようとしたとき、私はというエラーを取得しています

..私のナビゲーションを作りたいです。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     SqlConnection cn = new SqlConnection(@"Data Source=ABID-PC\SqlExpress;Initial Catalog=SamleNavigation;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("Select cat_Name from _NavParent", cn); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     TreeViewSample.DataSource = dt; 
     TreeViewSample.DataBind(); 


    } 
} 

任意の助けが理解されるであろう。

よろしくお願いします。 Abid。

+0

これは、 'TreeView'コントロールにバインドするデータは階層型でなければならないことを意味し、親ノードと子ノードを含む必要があります。例えば、 'XmlDataSource'かもしれません。 – Samich

+0

多くの場合、_ "絵は千語の価値がある"と言うことができる。しかし、このケースでは、代わりにあなたのコードの関連する部分を表示する方がはるかに優れています... – Nailuj

答えて

0

以下は可能な解決策です。 これは、ツリービューに便利なデータ型です。すべての

まず、あなたのCategoryクラス

// Your primary class 
public class Category { 
    public Int32 CategoryId { get; set; } 
    public Int32? ParentCategoryId { get; set; } 
    public String CategoryName { get; set; } 

    // List of children if any. 
    // In this example if your category doesn't have any children 
    // it is supposed to be null 
    public List<Category> Children { get; set; } 
    // The parent category. If this is your topmost category 
    // this property is deemed to be null. 
    public Category Parent { get; set; } 

    public override String ToString() { 
     return this.CategoryName; 
    } 
} 

それは親への参照と子のリストを持って作成する必要があります。カテゴリが一番上の場合、Parentプロパティはnullです。子がない場合は、nullでもかまいません。

次に、指定されたラッパーが必要です。ここ は、彼らは私がダミーデータで上記のコードを使用しました

// your primary tree 
public class CategoryTree : IHierarchicalEnumerable { 
    // Field to keep the collection data 
    private IEnumerable<Category> Categories; 
    // Empty constructor 
    public CategoryTree() { 
     //Init an empty list to avoid NullReferenceException 
     Categories = new List<Category>(); 
    } 
    // Primary constructor of our interest 
    public CategoryTree(IEnumerable<Category> categories) { 
     // Assign the collection data to the internal list 
     Categories = categories; 
    } 
    public IHierarchyData GetHierarchyData(object enumeratedItem) { 
     //cast the object 
     var cat = enumeratedItem as Category; 
     //create a new hierarchy item 
     return new CategoryHierarchyItem(cat); 
    } 

    public System.Collections.IEnumerator GetEnumerator() { 
     //IEnumerable support for foreach loop 
     return Categories.GetEnumerator(); 
    } 
} 

// Container that the TreeView control needs 
public class CategoryHierarchyItem : IHierarchyData { 
    // Internal field to store your data 
    private Category _Item; 

    // the constructor used to instantiate the class 
    public CategoryHierarchyItem(Category input) { 
     this._Item = input; 
    } 

    // returns another tree with children if any 
    public IHierarchicalEnumerable GetChildren() { 
     if (_Item.Children != null) 
      return new CategoryTree(_Item.Children); 
     return new CategoryTree(); 
    } 

    // The boolean value indicating weather this is the deepest level of your 
    // categories 
    public Boolean HasChildren { 
     get { return _Item.Children != null; } 
    } 

    // Actual data item 
    public Object Item { 
     get { return _Item; } 
    } 

    // This is the path. 
    // Construct it as you think is necessary. 
    // Here the format is the same as in namespaces of C# 
    public String Path { 
     get { 
      var sb = new StringBuilder(); 
      var current = _Item; 
      sb.Append(_Item.CategoryName); 
      while (current != null) { 
       sb.Insert(0, "."); 
       sb.Insert(0, current.CategoryName); 
       current = current.Parent; 
      } 
      return sb.ToString(); 
     } 
    } 

    public String Type { 
     get { return "Category"; } 
    } 

    // returns the parent of the current item 
    public IHierarchyData GetParent() { 
     if (_Item.Parent != null) 
      return new CategoryHierarchyItem(_Item.Parent); 
     return null; 
    } 
} 

です。これはほぼ同じように見えます。

protected void Page_Load(object sender, EventArgs e) { 

     // The topmost category 
     var top = new Category() { 
      CategoryId = 1, 
      CategoryName = "Top category" 
     }; 
     // Prepare children for them 
     var children = new List<Category>(); 
     // assign the children 
     top.Children = children; 
     // Populate the child list 
     children.Add(new Category { 
      CategoryId = 2, 
      CategoryName = "First child", 
      Parent = top 
     }); 
     children.Add(new Category { 
      CategoryId = 3, 
      CategoryName = "Second child", 
      Parent = top 
     }); 
     //Add another sub child 
     var thirdLevel = new List<Category>(); 
     thirdLevel.Add(new Category { 
      CategoryId = 4, 
      CategoryName = "Sub sub category", 
      Parent = children[0] 
     }); 
     // assign it to the first child 
     children[0].Children = thirdLevel; 
     // Make up a new tree 
     var tree = new CategoryTree(new[] { top }); 
     //Bind the source. 
     TreeView1.DataSource = tree; 
     // Bind it and enjoy. 
     TreeView1.DataBind(); 
    } 

可能な限りソリューションを変更できます。 ロジックをデータアクセスとUIツリービルディングに分割する必要があります。

+0

次にサブカテゴリのbカテゴリがあればどうなりますか? カテゴリの名前をここにハードコードすると、これは可能な解決策だとは思われません。 何と言いますか? –

+0

私はいつでも彼がしたいときにユーザーによって変更することができるn番目のカテゴリのソリューションが欲しい.. –

+0

@AbidAli注意を払わないでください。私の「ハードコードデモ」。これは単なる例です。あなたの実生活のコードは、一番上のカテゴリリストと再帰的に子どもを配置する必要があります。深さは、あなたが望むほど大きくすることができます。文字通り「永遠」です。 – Oybek

関連する問題