代码如下:
private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemIndex>=0)
{
DropDownList myddl=new DropDownList();
myddl=(DropDownList)e.Item.FindControl("ddl"); ds1=users.createconn("select type,id from product_type","t"); myddl.DataSource=ds1;
myddl.DataMember="t";
myddl.DataTextField="type";
myddl.DataValueField="id";
myddl.DataBind();
myddl.SelectedValue=e.Item.Cells[1].ToString(); }
}每次运行到“myddl.DataSource=ds1;”就报错,提示“未将对象引用设置到对象的实例”
不知道是哪里出了问题,忘高手指点一下。
先行谢过。

解决方案 »

  1.   

    ds1=users.createconn("select type,id from product_type","t");===>> ds1为空的!
      

  2.   

    e.Item.FindControl("ddl")有没有ddl控件阿?
      

  3.   

    e.item.cells[ddl的列序数].findcontrol[]试验下
      

  4.   

    看看这样行不行
    if (e.Item.ItemIndex>=0)
    {
    for(int j=0;j<e.item.count;j++)
    { {
    DropDownList myddl=new DropDownList();
    myddl=(DropDownList)e.Item[j].FindControl("ddl"); ds1=users.createconn("select type,id from product_type","t"); myddl.DataSource=ds1;
    myddl.DataMember="t";
    myddl.DataTextField="type";
    myddl.DataValueField="id";
    myddl.DataBind();
    myddl.SelectedValue=e.Item.Cells[1].ToString(); }}
    }
      

  5.   

    多谢楼上各位的回复,我可以肯定地告诉大家,ds1不可能为空。里面连接数据库的函数是我在类中定义的,所以在此段代码中没有给出。
    TO  Toti(as):还是报一样的错