简体   繁体   English

如何使用复选框节点填充树列表视图

[英]how do populate treelist view with checkbox nodes

Guys i want to populate a tree list view with nodes using the data from my database table (sql server) by looping it can anybody can give some idea. 伙计们,我想使用我的数据库表(SQL Server)中的数据通过循环来用节点填充树列表视图,任何人都可以给出一些想法。 I don't know where to start my coding. 我不知道从哪里开始编码。 im using this code to get data and connect to the database. 即时通讯使用此代码来获取数据并连接到数据库。 and the treelist view is at the winform. 树形列表视图位于winform。

SqlConnection cn = new SqlConnection();
SqlCommand cmd4 = new SqlCommand();
con.OpenConnections();
cmd4.Connection = cn;
cmd4.CommandType = CommandType.Text;
cn.ConnectionString = con.connections1;
cmd4.CommandText = "Select cmodname from modules";

don't know what to use next. 不知道接下来要用什么。 reader or datatable? 读者还是数据表?

Should be something like this: 应该是这样的:

You need to make checks for null's and stuff you don't want to appear. 您需要检查不希望出现的null和东西。

private void FillTreeView(string connectionString)
{
    string query = "Select cmodname from modules;";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();
        SqlDataReader sqlReader = command.ExecuteReader();
        try
        {
            while (sqlReader.Read())
            {
                     if (treeView2.SelectedNode != null)
                     {
                         treeView2.SelectedNode.Nodes.Add(sqlReader[0]);
                         treeView2.ExpandAll();
                     }
                     else
                     {
                         treeView2.Nodes[0].Nodes.Add(sqlReader[0]);
                     }

          }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            sqlReader.Close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM