62,046
社区成员
发帖
与我相关
我的任务
分享
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//找到Dropdownlist控件;
//DropDownList Type_Ddl = (DropDownList)e.Row.FindControl("Type_Ddl");
//Type_Ddl.SelectedValue = ((HiddenField)e.Row.FindControl("Type_Hdf")).Value;
//DropDownlist的ListItem项必须包含数据库中全部的项,否则会出现"异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。"错误;
//还要注意FindByValue,Items.FindByText的区别,如果下拉列表框由value就用FindByValue;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{
DataRowView rowItem = (DataRowView)e.Row.DataItem;
DropDownList clsName = (DropDownList)e.Row.FindControl("Type_Ddl");
if (rowItem["type"] != DBNull.Value)
{
clsName.Items.FindByText(rowItem["type"].ToString()).Selected = true;
}
}
}
}