0
不同當窗體上動態地添加用戶控件我有一個正確的輸出中添加控制動態表格是從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.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動態添加控件時,它不顯示相同的輸出?
我的問題是在FlowLayoutPanel中它重複的記錄。 –
謝謝。那樣簡單,我就把所有事情都弄複雜了。 :) –