如何使用LIST绑定数据库?

时间:2013-06-21 12:18:01

标签: asp.net sql radtreeview radtreelist

嗨iam尝试获取数据库数据并在RadTreeview中显示, 这里有一个样本,样本就像默认样本一样,每个Root和Child都必须手动输入, 但是我想从数据库绑定,值会自动填充(root / child)

我将展示示例代码,

List<SiteDataItem> siteData = new List<SiteDataItem>();

        siteData.Add(new SiteDataItem(1, 0, "Products"));
        siteData.Add(new SiteDataItem(2, 1, "RadControls for ASP.NET Ajax"));
        siteData.Add(new SiteDataItem(3, 1, "RadControls for Silverlight"));
        siteData.Add(new SiteDataItem(4, 2, "RadGrid"));
        siteData.Add(new SiteDataItem(5, 2, "RadScheduler"));
        siteData.Add(new SiteDataItem(6, 2, "RadEditor"));
        siteData.Add(new SiteDataItem(7, 3, "RadGrid"));
        siteData.Add(new SiteDataItem(8, 3, "RadMenu"));
        siteData.Add(new SiteDataItem(9, 3, "RadEditor"));

       RadTreeView1 .DataTextField = "Text";
       RadTreeView1.DataFieldID = "ID";
       RadTreeView1.DataFieldParentID = "ParentID";
       RadTreeView1.DataSource = siteData;
       RadTreeView1.DataBind();

请帮我做我想要的事情

2 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

protected void Page_Load(object sender, System.EventArgs e)
{
    List<TestModels> models = new List<TestModels>();

    models.Add(new TestModels() { ID = 1, ParentID = null, Text = "a1" });
    models.Add(new TestModels() { ID = 2, ParentID = 1, Text = "a2" });
    models.Add(new TestModels() { ID = 3, ParentID = 2, Text = "a3" });
    models.Add(new TestModels() { ID = 4, ParentID = null, Text = "a4" });
    models.Add(new TestModels() { ID = 5, ParentID = 4, Text = "a5" });


    RadTreeView1.DataTextField = "Text";
    RadTreeView1.DataFieldID = "ID";
    RadTreeView1.DataFieldParentID = "ParentID";
    RadTreeView1.DataSource = models;
    RadTreeView1.DataBind();
}

public class TestModels
{
    public int ID { get; set; }
    public int? ParentID { get; set; }
    public string Text { get; set; }
}

请在ParentID中将NULL设置为0。
因为它将0视为值。 RadTreeView无法找到任何具有0值的ID字段。

答案 1 :(得分:0)

你可以尝试这样的事情。

DataTable data= GetDataFromDatabase()
RadTreeView1.DataTextField = "TextFieldwhichyouwantshow";
RadTreeView1.DataFieldID = "IDFromdatabase";
RadTreeView1.DataFieldParentID = "ParentIDFromDatabase";
RadTreeView1.DataSource = data;
RadTreeView1.DataBind();