我有一个由ObjectDataSource填充的grideview。在EditItemTemplate中,我有一个下拉列表,它通过RowDataBound事件后面的代码填充:
ddStates.DataSource = snm.getStates();
ddStates.DataTextField = "State";
ddStates.DataValueField = "StateID";
ddStates.DataBind();
现在,我想要做的是将我的下拉列表的选定索引设置为objectDataSource中行的当前值。如何从ObjectDataSource获取StateID?
谢谢,我一直在这个圈子里跑。
答案 0 :(得分:5)
您可以随时使用行DataItem
来获取基础DataSource
:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit))
{
// if this doesn't work, use the debugger to get the type of e.Row.DataItem
DataRow row = ((DataRowView)e.Row.DataItem).Row;
int StateID = row.Field<int>("StateID");
var ddStates = (DropDownList)e.Row.FindControl("ddStates");
ddStates.DataSource = snm.getStates();
ddStates.DataTextField = "State";
ddStates.DataValueField = "StateID";
ddStates.DataBind();
ddStates.SelectedValue = StateID.ToString();
}
}