我只想在行编辑模式中检测到该网格视图,此时我只想绑定下拉列表。我找到了很多文章,我发了这个代码
我有这个错误
DataBinding:' AllProjects_Result'不包含属性 名称' Projects'。
protected void GVAllProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddlDistrictName = null;
DropDownList ddlPurposeName = null;
DropDownList ddlTypes = null;
if (e.Row.RowType == DataControlRowType.Footer && e.Row.RowState == DataControlRowState.Edit)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictName") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeName") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypes") as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictName") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeName") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypes") as DropDownList;
}
if (ddlDistrictName != null)
{
using (AlamaarRealEstateEntities context = new AlamaarRealEstateEntities())
{
ddlDistrictName.DataSource = context.Districts.ToList();
ddlDistrictName.DataTextField = "DistrictName";
ddlDistrictName.DataValueField = "ID";
ddlDistrictName.DataBind();
ddlPurposeName.Items.Insert(0, new ListItem(""));
ddlPurposeName.DataSource = context.Purposes.ToList();
ddlPurposeName.DataTextField = "PurposeName";
ddlPurposeName.DataValueField = "ID";
ddlPurposeName.DataBind();
ddlTypes.Items.Insert(0, new ListItem(""));
ddlTypes.DataSource = context.Types.ToList();
ddlTypes.DataTextField = "TypeName";
ddlTypes.DataValueField = "ID";
ddlTypes.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
ddlDistrictName.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "DistrictID")).ToString();
ddlPurposeName.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "PurposeID")).ToString();
ddlTypes.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "TypeID")).ToString();
}
}
}
这些行的错误:
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
ddlDistrictName.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "DistrictID")).ToString();
ddlPurposeName.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "PurposeID")).ToString();
ddlTypes.SelectedValue = (DataBinder.Eval(e.Row.DataItem, "TypeID")).ToString();
}
答案 0 :(得分:0)
检查DataControlRowState
事件中的RowDataBound
。
protected void GVAllProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//check if the edit state > 0
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//do stuff with the row that is being edited
}
}
}