0

我有这个方法:返回类别对象的列表,我想将此列表输出设置为 C# 中的下拉列表数据源,我该怎么做

public List<Category> GetAllCategories()
        {
            SqlConnection con = new SqlConnection(connectonstring);
            SqlCommand cmd = new SqlCommand("GetAllCategories", con);
            cmd.CommandType = CommandType.StoredProcedure;

            List<Category> Categories = new List<Category>();
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {                  
                    Category cat = new Category();
                    cat.JobCategoryid = Convert.ToInt32(reader["JobCategoryid"]);
                    cat.CategoryName = reader["categoryName"].ToString();
                    Categories.Add(cat);
                }
                reader.Close();
                return Categories;

            }
            catch (SqlException err)
            {
                return null;
            }
            finally
            {
                con.Close();
            }

        }
4

1 回答 1

1

像这样:

dropdownList.DataSource = GetAllCategories();
dropdownList.DateTextField= "CategoryName";
dropdownList.DataValueField = "JobCategoryid";
dropdownList.DataBind();
于 2013-06-28T15:23:48.193 回答