C# 更新下拉列表时出现问题

C# 更新下拉列表时出现问题,c#,drop-down-menu,C#,Drop Down Menu,更新和更改dropdownlist中的项目时出现问题 我有一个dropdownlist,它填充在代码隐藏文件中 代码如下: departmentComm = new SqlCommand("SELECT DepartmentID, Department FROM Departments", conn); conn.Open(); reader = departmentComm.ExecuteReader(); // Populate the list of categories departme

更新和更改dropdownlist中的项目时出现问题

我有一个dropdownlist,它填充在代码隐藏文件中

代码如下:

departmentComm = new SqlCommand("SELECT DepartmentID, Department FROM Departments", conn);
conn.Open();
reader = departmentComm.ExecuteReader();
// Populate the list of categories
departmentList.DataSource = reader;
departmentList.DataValueField = "DepartmentID";
departmentList.DataTextField = "Department";
departmentList.DataBind();
// Close the reader
reader.Close();
我从此列表中选择一个值并将其保存在我的employee表中。现在,假设我想更新employee表中的这个值


现在我的问题是,如何在dropdownlist中提取此值并显示它?

如果您只是想获取所选的部门id:

departmentList.SelectedValue
给你身份证

如果将下拉列表嵌入到datagrid中,则需要使用DataAdapter更新数据库并调用其更新方法


如果你需要更多的细节,你需要提供更多的细节。

什么语言?什么GUI框架?什么样的控制?这里没有足够的信息来回答这个问题。我正在研究c。我只是想知道如何使用数据库sqlcommand=new-SqlCommandSELECT Department从EmployeeID=@EmployeeID,connection;这为我提供了部门值,现在我如何使用该值作为dropdownlist中的选定值。如果我使用departmentList.SelectedItem.Text=reader[Department].ToString;虽然它改变了dropdownlist中选择的值,但由于我不知道该值的索引,该值改变了上次选择的索引的值。e、 g如果上一个选定项目的索引为0,则它的选定值将使用索引0。请仔细阅读:使用SelectedValue而不是SelectedItem.Text!这将为您提供ID。您不需要要使用ID的索引。