0

我有一个由 ObjectDataSource 填充的 grideview。在 EditItemTemplate 中,我有一个下拉列表,它通过 RowDataBound 事件后面的代码填充:

 ddStates.DataSource = snm.getStates();
 ddStates.DataTextField = "State";
 ddStates.DataValueField = "StateID";
 ddStates.DataBind();

现在,我想做的是将我的下拉列表的选定索引设置为来自 objectDataSource 的行的当前值。如何从 ObjectDataSource 获取 StateID?

谢谢,我一直在这个圈子上跑来跑去。

4

1 回答 1

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();
    }
}
于 2012-04-23T20:43:16.113 回答