2012-08-31 21 views
0

在我的GridView中,我有以下欄目:下拉在數據視圖

<Columns> 
    <asp:BoundField DataField="report_type" HeaderText="Report Type" 
     SortExpression="report_type" /> 

    <asp:BoundField DataField="progress" HeaderText="Progress" 
     SortExpression="progress" /> 

    <asp:TemplateField HeaderText=".."> 
     <ItemTemplate> 
      <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="progress"> 
       <asp:ListItem Value="0">Incomplete</asp:ListItem> 
       <asp:ListItem Value="1">Complete</asp:ListItem> 
      </asp:DropDownList> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 

進度欄只是在那裏它最終會被刪除演示目的。如何獲得進度的價值以在下拉列表中選擇正確的商品列表?

所以如果progress的值是1,下拉應該有Complete選中。如果progress的值爲0,則應選擇下拉菜單Incomplete

+0

它是數據視圖或的DetailView? –

+0

對不起,GridView。 – oshirowanen

回答

1

一個OnRowDataBound屬性添加到GridView在.aspx頁面:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler"> 

更換

<asp:BoundField DataField="Progress" HeaderText="Progress" 
    SortExpression="progress" /> 

<asp:TemplateField> 
    <ItemTemplate>    
     <asp:Label ID="progress_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Progress").ToString()%>'/> 
    </ItemTemplate>      
</asp:TemplateField> 

在後面的代碼:

protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     Label flag = (Label)e.Row.FindControl("progress_Flags"); 
     DropDownList myDropDown = (DropDownList)e.Row.FindControl("DropDownList1"); 
     if (flag.Text == "1") 
     { 
      myDropDown.SelectedValue = "1"; 
     } 
    //add more conditions here.. 

    }   
} 
0

RowDataBound事件時,可以使用e.Row.FindControl

protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    GridViewRow row = e.Row; 
    DataRowView dr = row.DataItem as DataRowView; 
    // now find the control in the row by control ID 
    DropDownList myDropDown = row.FindControl("DropDownList1") as DropDownList; 
}