2012-04-23 155 views
0

我有一個正在由ObjectDataSource填充的grideview。在EditItemTemplate中,我有被填充直通後面的代碼在RowDataBound事件一個下拉列表:在gridview中設置dropdownlist selectedIndex

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

現在,我想要做的是把我的下拉列表中選擇的指數的當前值的行來自objectDataSource。我如何從ObjectDataSource獲取StateID?

謝謝,我一直在這個圈子上運行。

回答

4

你總是可以使用行的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(); 
    } 
} 
+0

這就是它!這樣可行!非常感謝你,我正在砸我的腦袋試圖谷歌的信息! – Limey 2012-04-23 20:56:05

相關問題