日期:2014-05-18  浏览次数:20837 次

DataView.RowFilter怎么用呀
DataView dvTree = new DataView(ds.Tables[0]);
  dvTree.RowFilter =(PName=ParentID);

PName是方法传传进来的参数
ParentID是变量,两个都是字符形,运行时提示

筛选表达式“1”的计算结果不是布尔值项。
各位帮我看看!!
C# code

public static void treeView增加内容(string id, string name, string PName,string table, TreeView tv)
        {
            using (SqlConnection cn = Conn.connMsSql())
            {
                cn.Open();
                SqlCommand cmd = cn.CreateCommand();
                cmd.CommandText = "select "+id+" ,"+name+" ,"+PName+" from "+table;
               // cmd.Parameters.Add( new SqlParameter( "@id",id ));
               // cmd.Parameters.Add(new SqlParameter("@name",name));
              //  cmd.Parameters.Add(new SqlParameter("@PName", PName));
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);//得到DataSet
                AddTree(null, (TreeNode)null,PName,ds,tv);
            }
            
        }

   static void AddTree(string ParentID, TreeNode pNode, string PName, DataSet ds, TreeView tv)
        {
           
            TreeNode tn1 = new TreeNode();
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter =(PName=ParentID);
            foreach (DataRowView Row in dvTree)
            {
                if (pNode == null)
                {
                    tn1.Text = Row[1].ToString();//row[1]表示name;
                    tv.Nodes.Add(tn1);
                    string id = Row[0].ToString();
                    AddTree(id, tn1, PName, ds, tv);
                }
                else
                {
                    TreeNode tn2 = new TreeNode();
                    tn2.Text = Row[1].ToString();
                    pNode.Nodes.Add(tn2);
                    string id = Row[0].ToString();
                    AddTree(id, tn2, PName, ds, tv);    //再次递归
                }
                
            }
        }



------解决方案--------------------
试试这样 dvTree.RowFilter = PName + "="+ ParentID;