我的代码是这样的
<asp:datagrid id="dgd_adm" runat="server" Height="200px" Width="616px" PageSize="5" AllowPaging="True"………………>
<Columns>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="所在系">
<HeaderStyle Width="3cm"></HeaderStyle>
<ItemTemplate>
<%#Container.DataItem=department%>
</ItemTemplate>
<EditItemTemplate>
         <asp:DropDownList Runat="server" ID="DropDownList1" />
</EditItemTemplate>
////////
department 为表student 中的字段
C#中这样写的
mysql="select *from student";
DataSet ds=this.GetDataSet(mysql);//GetDataSet执行后返回mysql执行结果
ddg.DataSource=ds;//ddg 为DataGrid
ddg.DataMember=ds.Tables[0].ToString();
ddg.DataBind();
///
小弟刚刚接触.net还忘高人指点一下。谢谢

解决方案 »

  1.   

    DropDownList1.DataSource=ds;
    DropDownList1.DataMember=ds.Tables[0].ToString();
    DropDownList1.DataBind();
      

  2.   

    但是怎么样引用DropDownList1啊,它可是DataGrid中的控件好想不能这样哈。
    你能不能说的详细点
      

  3.   

    http://www.cnblogs.com/lovecherry/archive/2005/03/25/125525.html
      

  4.   

    private void DataGrid1_ItemDataBound(object sender,DataGridItemEventArgs e)
    {
    string sql = "SELECT name FROM sysobjects WHERE name LIKE 'KT_%'"; if(e.Item.ItemType==ListItemType.EditItem)
    {
    DropDownList ddl=(DropDownList)e.Item.FindControl("Procedure");
    dropdownlistBindData.BindData(ddl,sql,"name",conn); try
    {
    ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"存储过程名"))).Selected=true;//选择数据库内的作为默认
    }catch{}
    }
    if(e.Item.ItemType==ListItemType.Footer)
    {
    DropDownList ddl=(DropDownList)e.Item.FindControl("InsertProcedure");
    dropdownlistBindData.BindData(ddl,sql,"name",conn);
    }
    } public void DataGrid_Command(Object sender, DataGridCommandEventArgs e) 
    {
    switch(((Button)e.CommandSource).CommandName)
    {
    case "Insert":
    try
    {
    if(((TextBox)e.Item.FindControl("InsertEvent")).Text!="")
    {
    string sql_add = "INSERT INTO _List_Event (事件名,存储过程名)VALUES('"+((TextBox)e.Item.FindControl("InsertEvent")).Text+"','"+((DropDownList)e.Item.FindControl("InsertProcedure")).SelectedValue+"')";
    operateDataBase.SqlExecuteNonQuery(sql_add,conn);
    DataGridDataBind();
    }
    }catch{}
    break;
    case"Cancel":
    this.DataGrid1.EditItemIndex=-1;
    DataGridDataBind();
    break;
    case"Edit":
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    DataGridDataBind();
    break;
    case"Update":
    if(e.Item.ItemType==ListItemType.EditItem)//只有在编辑按下以后才能提交
    {
    try
    {
    string sql_update = "update _List_Event set 事件名='"+((TextBox)e.Item.FindControl("Event")).Text+"',存储过程名='"+((DropDownList)e.Item.FindControl("Procedure")).SelectedValue+"' where EventId='"+this.DataGrid1.DataKeys[e.Item.ItemIndex]+"'";
    operateDataBase.SqlExecuteNonQuery(sql_update,conn);
    this.DataGrid1.EditItemIndex=-1;
    DataGridDataBind();
    }catch{}
    }
    break;
    default:
    break;
    }
    }
    }
      

  5.   

    DropDownList1.DataSource=ds;
    DropDownList1.DataTextField="department";
    DropDownList1.DataValueField="depid";
    DropDownList1.DataBind();
      

  6.   

    楼主还是用上面建议的方法吧。
    <%#Container.DataItem=department%>
    这样的绑顶不对
    C#  <%# DataBinder.Eval(Container.DataItem,"department")%>
    VB <%# Container.DataItem("department")%>
      

  7.   

    回复 LoveCherry&koenemy
    按照2位朋友给的例子我写我的DataGrid1_ItemDataBound()为以下代码
    private void dgd_adm_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConnection conn=this.mypictoall.GetConnection();  
    DataSet ds=this.mypictoall.GetDataSet("select * from department");  
    if(e.Item.ItemType==ListItemType.EditItem)
    {
     ddl=(DropDownList)e.Item.FindControl("DropDownList1"); 
    ddl.DataSource=ds.Tables[0].DefaultView;   
    ddl.DataTextField="dep_name";
    ddl.DataValueField="dep_id";
    ddl.DataBind();
    ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"dep_id"))).Selected=true;//选择数据库内的作为默认
    }
    }
    但还是不行,跟踪发现if(e.Item.ItemType==ListItemType.EditItem)总是不成立。
    将其改为if(e.Item.ItemType!=ListItemType.EditItem)跟踪发现FindControl("DropDownList")总是返回空。
    回复 singlepine& qwerttyy
    你们说的
    DropDownList1.DataSource=ds;
    DropDownList1.DataTextField="department";
    DropDownList1.DataValueField="depid";
    DropDownList1.DataBind();
    在次如何引用DropDownList1啊????
    还望大家再说详细点,小弟谢谢了!