2016-07-01 23 views
1
private void button1_Click(object sender, EventArgs e) 
{ 
    con.Open(); 
    SqlDataAdapter da = new SqlDataAdapter(
     "select CustomerName,CompanyName,ContactNo from tbl_LeadFollowUp where WorkType='internet shopping'", con); 
    DataSet ds = new DataSet(); 
    DataTable dt = new DataTable(); 
    da.Fill(ds); 
    da.Fill(dt); 

    foreach (DataRow row in dt.Rows) 
    { 
     textBox1.Text = (row["CompanyName"].ToString()); 
     textBox2.Text = (row["CustomerName"].ToString()); 
     label2.Text = (row["ContactNo"].ToString()); 
    } 
    con.close(); 
} 
+0

瞭解數據綁定和如何使用數據綁定控件 –

+1

是這裏的問題:「我怎麼在UI顯示多行?」 - 因爲...你可能會想要一個類似的網格('DataGridView'等)把結果放在... –

+0

我不確定你是否要求這個,但是'label2.Text + =(row [「ContactNo」]。ToString());'。如果你想在標籤中顯示多個數據(就像你在問題中所說的那樣)使用這個。 – Berkay

回答

1

您必須使用DataGridview將數據從數據庫綁定到UI。

你不能迭代多個數據到單個標籤。它會得到寫得過多追加

private void button1_Click(object sender, EventArgs e) 
{ 
    con.Open(); 
    SqlDataAdapter da = new SqlDataAdapter("select CustomerName,CompanyName,ContactNo from tbl_LeadFollowUp where WorkType='internet shopping'", con); 
    DataSet ds = new DataSet(); 
    DataTable dt = new DataTable(); 
    da.Fill(ds); 
    da.Fill(dt); 
    dataGridView1.DataSource = da; 
    dataGridView1.DataMember = "Lead"; 
    con.close(); 
} 
相關問題