C# 数据视图中的下拉列表

C# 数据视图中的下拉列表,c#,.net-3.5,asp.net-3.5,dataview,C#,.net 3.5,Asp.net 3.5,Dataview,在我的GridView中,我有以下列: <Columns> <asp:BoundField DataField="report_type" HeaderText="Report Type" SortExpression="report_type" /> <asp:BoundField DataField="progress" HeaderText="Progress" SortExpression

在我的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
,则下拉列表应选择
complete

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;
}

在.aspx页面的gridview中添加OnRowDataBound属性:

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

是DataView还是DetailView?
<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..

    }          
}