ListItem item = new ListItem();
item.Text = Session["w"].ToString();
item.Value = Session["eid"].ToString();
DropDownList s = (DropDownList)GridView1.FindControl("DropDownList1");
s.Items.Add(item);报错为:最后一句
未将对象引用设置到对象的实例。请问如何解决

解决方案 »

  1.   

    具体到GridViewRow在FindControl("DropDownList1")GridView1.Rows[行号].FindControl("DropDownList1")
      

  2.   

    设置个断点看看是不是没有找到DropDownList1控件
    如果你是在gridview_RowDataBound事件中
    那么试试这样查找控件
    if(e.Row.RowType==DataControlRowType.DataRow)
    {
        DropDownList ddList = e.Row.FindControl("DropDownList1") as DropDownList;
    }
      

  3.   

    GridView1.FindControl --不能直接通过GridView1查找控件,因为一个多行的GridView会有多个DropDownList1控件。
    要在row上查找,如GridView1.Rows[0].FindControl("DropDownList1"); 
      

  4.   

    DropDownList ddList = this.GridView1.FindControl("DropDownList1") as DropDownList;
    DropDownList ddList.Items.Add(item);
    也不行能说能帮忘记改成功呀
      

  5.   

    DropDownList ddlist = (DropDownList)GridView1.Rows[0].FindControl("DropDownList1") as DropDownList;
    ddlist.Items.Add(item);改成这样后一样报错索引超出范围。必须为非负值并小于集合大小。
    参数名: index
      

  6.   

      DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("ddl ") as DropDownList;
    还有就是试用session时要判断下
    if(Session["w"]!=null &&Session["eid"]1=null)
    {
     ....
    }
      

  7.   

    楼上的报这个错
    CS0117: “System.EventArgs”并不包含“RowIndex”的定义
      

  8.   


    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        if (e.Row.RowType == DataControlRowType.DataRow)
          {
            DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList ;
          }   
      }