从asp下拉列表中的多个列中检索值

时间:2016-11-09 05:32:44

标签: c# asp.net listbox

我有一个下拉列表,其值从数据库中检索。我正在从数据库中检索ID和名称。

new Handler().post(new Runnable() {
            @Override
            public void run() {
                staggeredGridLayoutManager.scrollToPositionWithOffset(mImageList.size() - 1, 0);
            }
        });

我必须在文本框中显示每个部门的员工人数。假设用户选择人类部门,则文本框应显示该部门中的员工数量。

对于ListBox,只能检索数据库中的两个值。如何显示这种情况下的员工人数?

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) { }
        }

1 个答案:

答案 0 :(得分:1)

声明For the ListBox, only two values from the database can be retrieved.不正确。您可以使用任意多个字段填充数据表。但是,您可以像以前一样仅设置“列表框”项的“值”和“文本”属性。

  1. 更改存储过程代码以获取员工数。
  2. 将您的数据表dt标记为static and public
  3. 获取数据,您可以根据需要播放数据。您可以在listview上的文本框中获取员工计数,所选索引已更改,如下所示:

    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();
    
    }
    
相关问题