【发布时间】:2014-06-30 19:01:57
【问题描述】:
我是 asp.net 的新手。我正在构建一个管理页面,我想通过从数据库表中获取员工的数据在 GridView 中显示它。表有 3 列(id、name、isManager)。 “isManager”列有三个可能的值。这些值为“是”、“否”和“空”。管理员有权通过从下拉列表中选择“是”或“否”来决定让员工成为经理。
此管理页面有一个 GridView 控件,其中包含两个 BoundFiled(即 id 和名称)和一个模板字段(即 DropDownList)。我很难在 DropDownList 中显示“isManager”列值。如果数据库表行在“isManager”列中有“是”,我希望 DropDownList 将选定的值/文本显示为“是”,如果表行中有“否”,则显示“否”并显示一个项目“选择选择”如果table-row 包含一个空值。
我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select * from tblUsersTable";
DataSet ds = DataBaseConnectivity.GetData(query);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
// RowDataBound() method
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlManager = (e.Row.FindControl("ddlManager") as DropDownList);
DataSet ds = DataBaseConnectivity.GetData
("select distinct [isManager] from tblUsersTable");
ddlManager.DataSource = ds;
ddlManager.DataTextField = "isManager";
ddlManager.DataValueField = "isManager";
ddlManager.DataBind();
ddlManager.Items.Insert(0, new ListItem("Please select","-1"));
/* After these lines of code i am not finding the right way to implement my logic
*/
帮我弄清楚。
【问题讨论】:
-
旁注:您应该在调用
GridView1.DataBind();之前将DataSet加载到字段中,然后您可以从RowDataBound访问它,而无需在每一行中都加载它。 -
谢谢你真的很有帮助。愿上帝保佑(y)
标签: c# asp.net gridview drop-down-menu