protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             string sCgyNam = ((Label)e.Row.Cells[1].FindControl("lbCgyNam")).Text.Trim();
             DropDownList drop = (DropDownList)e.Row.FindControl("dropCgyNam");  // 找不到dropCgyNam           
                Function.BindDropDownList(drop, "nam", "id", "类别");
                //drop.Items.FindByText(sCgyNam).Selected = true;
            
        }
    }

解决方案 »

  1.   

    Function.BindDropDownList(drop, "nam", "id", "类别");
    这个是自己写的一个绑定DropDownList的函数----
    关键是怎么获得EditItem中ID为dropCgyNam的DropDownList
      

  2.   

    把你的.aspx中这个模版列的定义也贴出来,另外:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
    string sCgyNam = ((Label)e.Row.Cells[1].FindControl("lbCgyNam")).Text.Trim();
    DropDownList drop = (DropDownList)e.Row.FindControl("dropCgyNam");  // 找不到dropCgyNam            if(drop!=null)
    {
    Function.BindDropDownList(drop, "nam", "id", "类别");
    }
        }
    }
      

  3.   

    aspx:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                            Width="337px" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                            <Columns>
                                <asp:BoundField DataField="id" HeaderText="车号" />
                                <asp:TemplateField HeaderText="类型">
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="dropCgyNam" runat="server" Style="position: relative" Width="107px">  <<----------在这里
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lbCgyNam" runat="server" Text='<%# Bind("nam") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:CommandField ShowEditButton="True" />
                            </Columns>
                        </asp:GridView>
      

  4.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType == DataControlRowType.DataRow)
            {
                    //保存当前行的au_id的值
                    string au_id = this.GridView1.DataKeys[e.Row.RowIndex]["au_id"].ToString();
            
            //对DropDownList做数据绑定
                    DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");                SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=password;database=pubs");
                    string strSQL = "select au_id from authors";
                    SqlCommand cmd = new SqlCommand(strSQL, cn);
                    cn.Open();                dropTemp.DataSource = cmd.ExecuteReader();
            dropTemp.DataTextField = "au_id";
            dropTemp.DataBind();                //到DropDownList中根据au_id的值去找需要设置为选中状态的项目,将其设置为选中
                    ListItem item = dropTemp.Items.FindByText(au_id);
                    if(item != null)
                    {
                        item.Selected = true;
                    }
                    cn.Close();
            }
            }
      

  5.   

    你关键的问题在这里,没有指明单元格
    DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
      

  6.   

    TO:amandag(高歌) 
    //对DropDownList做数据绑定
    DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");-------这个也找不到EditItem里的DropDownList
      

  7.   

    你要学会看位置啊你的第一个是 <asp:BoundField DataField="id" HeaderText="车号" />所以现在就是
    DropDownList dropTemp = (DropDownList)e.Row.Cells[1].FindControl("dropTemp");
      

  8.   

    TO:amandag(高歌) 
    //对DropDownList做数据绑定
    DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
    -----
    这个是你的我的是:DropDownList drop = (DropDownList)e.Row.Cells[1].FindControl("dropCgyNam");
    单步执行完这句后发现drop 为null
      

  9.   

    问题是可以访问ItemTemplate中的drop 
    但是取不到EditTemplate中的drop
      

  10.   

    当然访问不到
    应该是:
    (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpCgyNam")