2015-05-15 19 views
0

我有這樣如何讓所有的女士數據庫數據[行]在動態文本框

int cLeft=0; 
public System.Windows.Forms.TextBox AddNewTextBox() 
{ 
    System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox(); 
    OleDbCommand command = new OleDbCommand(); 
    command.Connection = connection; 
    string query = " select * FROM DotMatrix; 
    command.CommandText = query; 
    OleDbDataReader reader = command.ExecuteReader(); 
    this.Controls.Add(txt); 
    txt.Top = (cLeft*25) + 124; 
    txt.Left = 50; 
    txt.Height = 20; 
    txt.Width = 259; 
    while (reader.Read()) 
    { 
     txt.Text=reader["Pertanyaan"].ToString(); 
    } 
    if (txt.Text=="") 
    { 
     MessageBox.Show("Pertanyaan Habis , Akan Redirect Ke Hasil"); 
    } 
    cLeft = cLeft + 1; 
    return txt; 
} 

private void textBox1_TextChanged_1(object sender, EventArgs e) 
{ 
    AddNewTextBox(); 
} 

我的問題代碼,爲什麼textBox中只顯示從數據庫1號線??? 我想在Pertanyaan行的數據顯示[行] 感謝回答

回答

0

這條線是通過每一行循環和不斷改寫文本框的值:

while (reader.Read()) 
{ 
    txt.Text=reader["Pertanyaan"].ToString(); 
} 

所以同一文本框被分配到了並結束。

你的文本框創建代碼要被移動到內循環,這樣的事情:

while (reader.Read()) 
{ 
    System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox(); 
    txt.Top = (cLeft*25) + 124; 
    txt.Left = 50; 
    txt.Height = 20; 
    txt.Width = 259; 
    txt.Text=reader["Pertanyaan"].ToString(); 
    if (txt.Text=="") 
    { 
     MessageBox.Show("Pertanyaan Habis , Akan Redirect Ke Hasil"); 
    } 
    this.Controls.Add(txt); 
} 
相關問題