2013-07-23 82 views
-1

我使用包含下拉list.Now我不想寫相同的代碼從database.I獲取相同的數據C#使得在asp.net應用程序是嘗試這種代碼,但它不工作無法將數據與下拉列表綁定?

protected void Page_Load(object sender, EventArgs e) 
    { 

     DataTable DT = sel_obj.select_Dept_Name(); 
     departmentDrop.DataSource = DT; 
     departmentDrop.DataMember = "Department_Name"; 
     departmentDrop.DataBind(); 
    } 
public DataTable select_Dept_Name() 
    { 
     module c = new module(); 
     c.DB_Connection(); 

     if (c.con.State == ConnectionState.Open) 
     { 
      c.con.Close(); 
      c.con.Open(); 
     } 

     DataSet DS = new DataSet(); 
     string QRY = ""; 
     QRY = "SELECT Department_Name FROM Department_Master"; 
     SqlDataAdapter DA = new SqlDataAdapter(QRY, c.con); 
     DA.Fill(DS); 
     DataTable DT = DS.Tables[0]; 
     return DT; 
    } 
+0

'不工作'不是一個正確的問題。請更詳細地說明您面臨的問題 –

+0

add'departmentDrop.ValueMember =「Department_Name」'and'departmentDrop.DataBind();':) – zey

回答

1

您需要調用「DataBind()」函數。您還需要確保您的表格包含要與下拉列表綁定的數據。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
     DataTable DT = sel_obj.select_Dept_Name(); 
     departmentDrop.DataSource = DT ; 
     departmentDrop.DataTextField = "Department_Name"; 
     departmentDrop.DataValueField = "Department_Name"; 
     departmentDrop.DataBind(); 
     } 
    }