2016-11-09 20 views
1

我有一個從數據庫中檢索值的下拉列表。我正在從數據庫中檢索ID和名稱。從asp中的多個列中檢索值drop down

public void GetDepartment_temp() 
     { 
      try 
      { 
       DataTable dt = new DataTable(); 
       listBoxDepartment.ClearSelection(); 
       Get_Department objDAL = new Get_Department(); 
       dt = objDAL.Get_Hospital_Department(); 
       if (dt != null && dt.Rows.Count > 0) 
       { 
        foreach (DataRow row in dt.Rows) 
        { 
         listBoxDepartment.Items.Add(new ListItem(row["department_name"].ToString(), row["department_id"].ToString())); 

        } 
       } 
      } 
      catch (Exception) { } 
     } 

我必須在文本框中顯示每個部門的僱員人數。假設用戶選擇人力部門,那麼文本框應顯示該部門的僱員人數。

對於ListBox,只能從數據庫中檢索兩個值。我如何顯示在這種情況下的員工人數?

public DataTable Get_Hospital_Department() 
     { 
      try 
      { 
       DataTable dt = new DataTable(); 
       dt = DbAccessHelper.ExecuteDataSet("p_get_hospital_department", true).Tables[0]; 
       return dt; 
      } 
      catch (Exception) { return null; } 
     } 

CREATE PROCEDURE [dbo].[p_get_hospital_department] 
AS 
BEGIN 
    SET NOCOUNT ON; 

    SELECT department_id 
     ,department_name 
    FROM [dbo].[tbl_hospital_department]; 
END 
+0

請分享您的代碼爲'Get_Hospital_Department()' –

+0

我提供的代碼和程序。 – user4221591

回答

1

聲明For the ListBox, only two values from the database can be retrieved.是不正確的。您可以使用填充數據表,並根據需要填寫許多字段。但是,您可以像設置完成一樣僅設置Listbox項目的Value和Text屬性。

  1. 更改存儲過程代碼以獲取員工數也。
  2. 將您的datatable dt標記爲static and public
  3. 取出數據表,你可以隨意玩數據。您可以在列表視圖中選擇索引文本框中改變獲取員工數,如下圖所示:

    public static DataTable dt = new DataTable(); 
    
    public void GetDepartment_temp() 
    { 
        try 
        { 
    
    
         string connString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString; 
         SqlConnection connection = new SqlConnection(connString); 
    
         SqlCommand command = 
          new SqlCommand(
           "select Department.DepartmentID, Department.[Department Name], count(Department.DepartmentID) as empCount from Department join Employee on Department.DepartmentID = Employee.DepartmentID group by Department.DepartmentID, Department.[Department Name]", 
           connection); 
    
         command.Connection.Open(); 
    
         SqlDataAdapter da = new SqlDataAdapter(command); 
         da.Fill(dt); 
         dt.PrimaryKey = new DataColumn[] {dt.Columns["DepartmentID"]}; 
         ListBox1.ClearSelection(); 
         if (dt != null && dt.Rows.Count > 0) 
         { 
          foreach (DataRow row in dt.Rows) 
          { 
           ListBox1.Items.Add(new ListItem(row["Department Name"].ToString(), 
            row["DepartmentID"].ToString())); 
    
          } 
         } 
        } 
        catch (Exception ex) 
        { 
        } 
    } 
    
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    
        DataRow dr = dt.Rows.Find(Convert.ToInt32(ListBox1.SelectedItem.Value)); 
        TextBox5.Text = dr["empCount"].ToString(); 
    
    } 
    
+0

user4221591它工作嗎? –

+0

感謝它的工作! – user4221591

+0

歡迎您:) –