0

在表单上动态添加用户控件时,我得到了正确的输出

图片

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
            using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
            {

                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                int y = 0;
                while (DR1.Read())
                {
                    y++;
                    for (int i = 0; i < y; i++)
                    {
                        UserControl2 userconrol = new UserControl2();
                        userconrol.Location = new Point(50, 30 * i);
                        userconrol.Tag = i;
                        userconrol.LastName = (string)DR1["LastName"];
                        this.Controls.Add(userconrol);
                    }
                }
            }
        }

但是当我使用 flowlayoutpanel 动态添加控件时,发生了这样的事情: 图片

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
            using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
            {

                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                int y = 0;
                while (DR1.Read())
                {
                    y++;
                    for (int i = 0; i < y; i++)
                    {
                        UserControl2 userconrol = new UserControl2();
                        userconrol.Tag = i;
                        userconrol.LastName = (string)DR1["LastName"];
                        flowLayoutPanel1.Controls.Add(userconrol);
                    }
                }
            }
        }

问题是什么 ?我使用相同的循环为什么当我使用 flowlayoutpanel 动态添加控件时它不显示相同的输出?

4

2 回答 2

1

将 FlowLayoutPanel 的FlowDirection()更改为 TopDown。

编辑:

另外,摆脱 for 循环......那应该做什么?

也许像什么?

    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Student", myDatabaseConnection))
        using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
        {
            int i = 0;
            SqlDataReader DR1 = SqlCommand.ExecuteReader();
            while (DR1.Read())
            {
                i++;
                UserControl2 userconrol = new UserControl2();
                userconrol.Tag = i;
                userconrol.LastName = (string)DR1["LastName"];
                flowLayoutPanel1.Controls.Add(userconrol);
            }
        }
    }
于 2013-06-04T01:44:56.630 回答
0
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
            using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
            {
                List<string> list1 = new List<string>();
                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                while (DR1.Read())
                {
                    list1.Add((string)DR1["LastName"]);
                }

                int i = 0;
                foreach (string LastName in list1)
                {
                    i++;
                    UserControl2 userconrol = new UserControl2();
                    userconrol.Tag = i;
                    userconrol.LastName = LastName;
                    flowLayoutPanel1.Controls.Add(userconrol);
                }
            }
        }
于 2013-06-04T04:02:37.470 回答