我在datagrid中加入一个模板列,模板列中加入一个dropdownlist,我想把dropdownlist绑定数据表中的一个字段(auditresult),模板列该怎样写。dropdownlist的item包括:1:通过;0:未通过。

解决方案 »

  1.   

    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);private void DataGrid1_ItemDataBound(object sender,DataGridItemEventArgs args)
    {
    if(args.Item.ItemType == ListItemType.EditItem)
    {
    DropDownList DropGroupID1 = (DropDownList)args.Item.FindControl("DropGroupID");
    if(DropGroupID1 != null)
    {
    SqlConnection conn = new SqlConnection(Global.ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("select tmpUID,tmpName from tmpFlag",conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    DropGroupID1.DataSource = dr;
    DropGroupID1.DataValueField = "tmpUID";
    DropGroupID1.DataTextField = "tmpName";
    DropGroupID1.DataBind();
    dr.Close();
    string asdd= ((Label)args.Item.FindControl("Label1")).Text;
    DropGroupID1.Items.FindByValue(asdd).Selected = true;
    }
    }
    }
      

  2.   

    上面的怎么那么复杂,不是用一个语句就可以了吗,只是我不知道该怎样写.
    下面是我写的代码,但是没有给我绑定数据
    <asp:TemplateColumn HeaderText="审核结论">
    ItemTemplate>
    <asp:DropDownList id=DropDownList1 runat="server" Width="80px" Value='<%#DataBinder.Eval(Container.DataItem,"auditresult")%>'>
    <asp:ListItem Value="1">通过</asp:ListItem>
    <asp:ListItem Value="0">未通过</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>