Telerik radtreeview 显示连接表中的数据给出错误这些列当前没有唯一值

Posted

技术标签:

【中文标题】Telerik radtreeview 显示连接表中的数据给出错误这些列当前没有唯一值【英文标题】:telerik radtreeview display data from joined tables gives error These columns don't currently have unique values 【发布时间】:2016-03-21 13:12:38 【问题描述】:

我想显示两个名为“OpenCourses”和“OpenCoursesChapters”的表中的数据。

Opencourses表:

OpenCoursesChapters表:

我想将 radtreeview 上的课程名称显示为父节点,将章节名称显示为子节点:

 <div class="demo-container size-thin">

    <telerik:RadTreeView Height="300px" runat="server" ID="RadPanelBar1" DataTextField="name" DataFieldID="id"
     DataFieldParentID="parentid" DataSourceID="SqlDataSource1" >

    </telerik:RadTreeView>
</div>

   <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:ConStr %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT courseid as id, NULL as parentid, coursename as name from opencourses union select chapterid as id, courseid as parentid, chaptername as name from opencourseschapters"></asp:SqlDataSource>

在运行项目时出现错误

这些列目前没有唯一值。

有什么想法吗?

【问题讨论】:

【参考方案1】:

首先你的选择命令必须是:

    "SELECT chapterid AS ID, chaptername AS Name,
courseid + 1000 AS parentID FROM OpenCoursesChapters p
UNION ALL
SELECT courseid + 1000 AS ID, coursename AS Name, null AS parentID
FROM Opencourses"

除非你可以像这样使用asp TreeView:

            <asp:TreeView ID="RadPanelBar1"
                 runat="server"
                ShowCheckBoxes="All" 
                PopulateNodesFromClient="False"
                 onclick="client_OnTreeNodeChecked();" >
            </asp:TreeView>

在java脚本中(可能在页脚)使用这个函数来自动检查子节点:

 function client_OnTreeNodeChecked(evt)

 

     var obj;

     if(window.event)    obj = window.event.srcElement;

     else                obj = (evt ? evt : (window.event ? window.event : null)).target;
     var treeNodeFound = false;
     var checkedState;
     if (obj.tagName == "INPUT" && obj.type == "checkbox" ) 
     
         checkedState = obj.checked;
         do
         
             obj = obj.parentNode;
          
         while (obj.tagName != "TABLE")

         var parentTreeLevel = obj.rows[0].cells.length;

         //get the current node's parent node.
         var tables = obj.parentNode.getElementsByTagName("TABLE");
         var numTables = tables.length;

         if (numTables >= 1)
         
             for (i=0; i < numTables; i++)
             
                 if (tables[i] == obj)
                 
                     treeNodeFound = true;
                     i++;
                     if (i == numTables) return;
                 

                 if (treeNodeFound == true)
                     var childTreeLevel = tables[i].rows[0].cells.length;
                     if (childTreeLevel > parentTreeLevel)
                         var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                         var inputs = cell.getElementsByTagName("INPUT");
                         inputs[0].checked = checkedState;
                     
                     else  return;
                 
             
         
     

  

所以,在 page_load() 服务器端代码后面调用这个函数:

 public void tree_fill_general(TreeView tree_name, string parent_query, string child_query)
    
        try
        
            if (your connection == false)
            
               your connection.Open();
            
            tree_name.Nodes.Clear();
            string q = parent_query;
            SqlDataAdapter sda = new SqlDataAdapter(q, your connection);
            DataSet datasetParent = new DataSet();
            sda.Fill(datasetParent);
            for (int i = 0; i <= datasetParent.Tables[0].Rows.Count - 1; i++)
            
                TreeNode main = new TreeNode();
                main.Text = datasetParent.Tables[0].Rows[i]["name"].ToString();
                main.Value = datasetParent.Tables[0].Rows[i]["code"].ToString();

                string q1 = child_query + main.Value;
                SqlDataAdapter sda1 = new SqlDataAdapter(q1, your connection);
                DataSet datasetChild = new DataSet();
                sda1.Fill(datasetChild);
                for (int j = 0; j <= datasetChild.Tables[0].Rows.Count - 1; j++)
                
                    TreeNode mainchild = new TreeNode();
                    mainchild.Text = datasetChild.Tables[0].Rows[j]["name"].ToString();
                    mainchild.Value = datasetChild.Tables[0].Rows[j]["code"].ToString();
                    main.ChildNodes.Add(mainchild);
                
                tree_name.Nodes.Add(main);
            

        
        catch (Exception eee)
        
            Utility.SetMessage(Page, "error", eee.Message, enumIcon.Error);
        
    

和父查询:

"select courseid as code,coursename as name from Opencourses  order by sec,courseid";

final , child_query :

"select chapterid as code,chaptername as name opencourseschapters where courseid  =";

【讨论】:

以上是关于Telerik radtreeview 显示连接表中的数据给出错误这些列当前没有唯一值的主要内容,如果未能解决你的问题,请参考以下文章

Telerik控件RasMaskedTextBox未显示错误消息

带有多个参数的存储过程的 Telerik 报告不显示结果

Telerik RadSpreadsheet如何显示分组?

c# WPF Telerik combobox 多选自定义显示值

如何显示 Telerik 图表的 steparea 系列的工具提示?

在 Telerik 报表的最后一页上的报表页脚之前显示页脚