2013-02-04 45 views
-1

從選擇填充有相應的行數據標籤我有一個C#的形式。我有一個組合框是控制器。我想填充標籤從同一行中我的下拉列表中選擇的項目顯示的字段。如何在組合框在C#的形式

例如,物品是我的組合框,所以如果我從ComboBox中選擇一個項目,然後我想「說明」來填充我的標籤,如選擇的項目組合框。

using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.Sql; //Dir used to connect to sql DB 
using System.Data.SqlClient; //Dir used to connect to sql DB 



namespace items_Form 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'syteLine_AppDataSet.item' table. You can move, or remove it, as needed. 
      this.itemTableAdapter.Fill(this.syteLine_AppDataSet.item); 
      //creating the connection to database. 
      SqlConnection con_str = new SqlConnection("Data Source=SL1;Initial Catalog=SyteLine_App;Integrated Security=True"); 
      SqlDataAdapter dta_ad = new SqlDataAdapter("SELECT item, description, lead_time, product_code, p_m_t_code, stocked, matl_type, family_code, low_level, days_supply, order_min, order_mult, plan_code,accept_req, shrink_fact, var_lead, decifld1, decifld2, decifld3, datefld, pass_req, order_max, uf_Active, uf_BoughtOff, uf_Old, uf_ProjectEngineer FROM dbo.item", con_str); 
      DataTable dta_tbl = new DataTable(); 

      dta_ad.Fill(dta_tbl); 


      for (int i = 0; i < dta_tbl.Rows.Count; i++) 

      { 
       cbxItem.Items.Add(dta_tbl.Rows[i]["item"]); 
      } 

     }   

     public void cbxItem_SelectedIndexChanged(object sender, EventArgs e) 
     { 


      if (cbxItem.SelectedIndex == -1) 
      { 
       label1.Text = string.Empty; 
      } 
      else 
      { 
       label1.Text = cbxItem.SelectedItem.ToString(); 
      } 
     } 
    } 
} 
+0

您之前已經兩次要求這同一個問題。 – Brian

回答

1

我只會檢索頁面加載1或2列,只是要顯示下拉列表。

我會做cbxItem_SelectedIndexChanged方法通過將選擇值的第二個查詢:

DataTable dt = SomeFunctionThatGetsTheDataForTheSelectedValue(cbxItem.SelectedValue) 
lblDescription = dt.Rows[0]["Description"];