高手快来看啊,.net代码出错了

来源:百度知道 编辑:UC知道 时间:2024/09/22 03:11:51
protected void GridViewQuery1_RowEditing(object sender, GridViewEditEventArgs e)
{
DataSet ds = DB_R.DbHelperSQL.Query("select * from t_wpgroup");
DropDownList ddl = (DropDownList)this.GridViewQuery1.Rows[e.NewEditIndex].FindControl("DropDownList1");
ddl.DataTextField = "fname";
ddl.DataValueField = "fitemid";
ddl.DataSource = ds;
ddl.DataBind();
ddl.SelectedValue = GridViewQuery1.DataKeys[e.NewEditIndex].Values[1].ToString();
}

报错:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

红色游标指向ddl.DataTextField = "fname";这行

这两句
ddl.DataTextField = "fname";
ddl.DataValueField = "fitemid";
写在
ddl.DataSource = ds;(最好写成ddl.DataSource = ds.Tables[0];)
的后边

DropDownList ddl = (DropDownList)this.GridViewQuery1.Rows[e.NewEditIndex].FindControl("DropDownList1");
这行代码有问题,ddl应该还是null没找到
后面应该不需要FindControl了

感觉和一楼一样

你跟一下看看第二行 ddl 有没有数据

DropDownList ddl = (DropDownList)this.GridViewQuery1.Rows[e.NewEditIndex].FindControl("DropDownList1");

这个ddl能这样new实例吗? 应该是DropDownList ddl = new DropDownList(参数)才可以把