質問

When adding usercontrol dynamically on the form i got a right ouput

Picture

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);
                    }
                }
            }
        }

But when i use flowlayoutpanel to add controls dynamically this is what happened: pic

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);
                    }
                }
            }
        }

What is the problem ? I use the same loop why is it when i use flowlayoutpanel to add control dynamically it doesn't show the same output?

役に立ちましたか?

解決

Change the FlowDirection() of your FlowLayoutPanel to TopDown.

Edit:

Also, get rid of the for loop...what is that supposed to be doing?

Maybe something like?

    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);
            }
        }
    }

他のヒント

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);
                }
            }
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top