0

以下の列を含むデータベース テーブル (PatientDetails という名前) があります。

PatientId not null,
title,
sex,
lastname,
birthday,
firstname,
middlename,
remarkline,
remarks

そして、上記の表を使用して、C#でPatientIdasParentと childnode をfirstnameaとして使用します。TreeViewどうやってやるの ?

以下のコードビハインドを作成しました。

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = RunQuery("select PatientId,firstname from PatientDetails");

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        TreeNode root = new TreeNode(
            ds.Tables[0].Rows[i][1].ToString(),
            ds.Tables[0].Rows[i][0].ToString());

        root.SelectAction = TreeNodeSelectAction.Expand;
        CreateNode(root);
        TVPatArc.Nodes.Add(root);
    }      
}

void CreateNode(TreeNode node)
{
    DataSet ds = RunQuery("Select PatientId,firstname "
                          + "from PatientDetails where PatientId ="
                          + node.Value);

    if (ds.Tables[0].Rows.Count == 0)
    {
        return;
    }

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        TreeNode tnode = new TreeNode(
            ds.Tables[0].Rows[i][1].ToString(),
            ds.Tables[0].Rows[i][0].ToString());

        tnode.SelectAction = TreeNodeSelectAction.Expand;
        node.ChildNodes.Add(tnode);
        CreateNode(tnode);
    }
}

DataSet RunQuery(String Query)
{
    DataSet ds = new DataSet();
    String connStr = "Data Source=LOCALHOST\\SQLEXPRESS;"
                     + "Initial Catalog=MyDatabase;"
                     + "User ID=sa;Password=sa";

    using (SqlConnection conn = new SqlConnection(connStr))
    {
        SqlCommand objCommand = new SqlCommand(Query, conn);
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        da.Fill(ds);
        da.Dispose();
    }

    return ds;
}

しかし、CreateNode(tnode);それはSystem.StackOverflowException. 無限ループが原因だと思います。確信はないけど。

4

2 に答える 2