C# 如何通过dataset循环特定行并设置为treeview

C# 如何通过dataset循环特定行并设置为treeview,c#,asp.net,C#,Asp.net,我想循环遍历每个数据行,检查pid的值,直到它得到0、null或空值……下面是我的代码 if (!string.IsNullOrEmpty(pIDstr)) { int patientID = Convert.ToInt32(pID); //string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

我想循环遍历每个数据行,检查pid的值,直到它得到0、null或空值……下面是我的代码

if (!string.IsNullOrEmpty(pIDstr))
{
    int patientID = Convert.ToInt32(pID);

    //string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
    string sqlquery = "SELECT * FROM [MyDatabase].[dbo].[PatExam] where PId = '" + patientID + "'";
    string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
    using(SqlConnection conn = new SqlConnection(connection))
    {
        //SqlConnection conn = new SqlConnection(con);
        DataSet ds;
        ds = new DataSet();
        SqlDataAdapter cmpatientexam;
        conn.Open();

        cmpatientexam = new SqlDataAdapter(sqlquery, conn);
        cmpatientexam.Fill(ds, "PatientExam");

        foreach (DataRow patrow in ds.Tables["PatientExam"].Rows)
        {

                TreeNode tvpatexam = new TreeNode();
                tvpatexam.Text = patrow["PId"].ToString();
                TreeView1.Nodes.Add(tvpatexam);

                for //loop for checking all the patrow["PId"] value
                {
                    TreeNode childtvpatexam = new TreeNode();
                    childtvpatexam.Text = patrow["Exam"].ToString();
                    tvpatexam.ChildNodes.Add(childtvpatexam);
                }
            //TreeView1.Nodes.Add(tvpatexam);
        }


        ds.Dispose();
        cmpatientexam.Dispose();
        conn.Close();
        conn.Dispose();
    }
}
这是怎么可能的任何人都可以发送代码…非常感谢

我的数据库表PatExam包含以下值

PId     Exam
1004    firstexam
1004    secondexam
1004    thridexam
1004    fourthexam
所以我希望1004作为父节点,1004的所有检查值作为树状视图中的子节点


怎么可能呢?

要为每个父节点添加子节点,请尝试以下操作

TreeNode pidNode = new TreeNode();
pidNode.Text = PIDstr;

foreach (DataRow patrow in ds.Tables["PatientExam"].Rows)
{
    TreeNode examType = new TreeNode();
    examType.Text = patrow["Exam"].ToString();
    pidNode.Nodes.Add(examType);
}

TreeView1.Nodes.add(pidNode);
无需检查
Null
0
值-不确定您在问什么

更新

如果重复添加该节点;这意味着用于生成节点列表的操作是重复的。您可以使用
TreeView1.Nodes.Clear()
删除所有节点,也可以检查特定节点是否存在

要删除所有节点,请尝试以下操作

TreeView1.Nodes.Clear();
要删除特定节点并刷新其内容,请在重新填充节点之前尝试类似的操作

TreeNode node = TreeView1.Nodes.FirstOrDefault(p=>p.Text == PIDstr);
TreeView1.Nodes.Remove(node);

我尝试以下操作…..如果(patrow[“PId”]==DBNull.Value | | patrow[“PId”].ToString()==“0”){TreeNode childtvpatexam=new TreeNode();childtvpatexam.Text=patrow[“Exam”].ToString();tvpatexam.ChildNodes.Add(childtvpatexam);}break;。。。。。。但是它没有进入if条件的内部……怎么了?@Pratik您在
{}
块之外有
中断
。循环在其check语句为true或第一次遇到
break
语句时结束。我已将{}块放入,如您所见(patrow[“PId”]==DBNull.Value | | patrow[“PId”].ToString()=“0”){TreeNode childvpatexam=new TreeNode();childtvpatexam.Text=patrow[“Exam”].ToString();tvpatexam.ChildNodes.Add(childtvpatexam);break;}但是它没有进入if条件的内部…y?@Pratik,因为if语句的条件没有满足。您没有
DBNull
或等于
0
的元素。检查您的数据。有patrow[“PId”]的数据,我可以在树状视图的根节点中看到patientid,但我想要patientid的childnode