填充的GridView的下拉列表我使用代碼如下:問題從數據集
protected void grdViewCInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
MySqlConnection objMycon1 = new MySqlConnection(strProvider);
objMycon1.Open();
MySqlCommand cmd1 = new MySqlCommand("select * from tblcountrynames",objMycon1);
MySqlDataAdapter da = new MySqlDataAdapter(cmd1);
DataSet ds = new DataSet();
da.Fill(ds);
// DropDownList Control Object Created to bind the data dynamically with each
// nested DropDownlist control placed inside the template column of the GridView
// Control.
DropDownList drdList;
// foreach loop is used to loop through each row of GridView Control.
foreach (GridViewRow grdRow in grdViewCInfo.Rows)
{
// Nested DropDownList Control reference is passed to the DrdList object.
// This will allow you access the properties of dropdownlist placed
// inside the GridView Template column.
drdList = (DropDownList)(grdViewCInfo.Rows[grdRow.RowIndex].FindControl("ddlCountry"));
// DataBinding of nested DropDownList Control for each row of GridView Control.
drdList.DataSource = ds;
drdList.DataValueField = "ID";
drdList.DataTextField = "Name";
drdList.DataBind();
}
}
它給出了一個錯誤爲:未設置爲一個對象的一個實例
對象引用。
在生產線drdList.DataSource = ds;
我該如何解決這個問題???
我包括列索引,從而錯誤消失,但下拉列表顯示在編輯事件 – user755230 2011-05-24 06:06:54
它不會在循環中爲我描繪出 – user755230 2011-05-24 06:15:33
,如果它沒有在循環中去空,你是如何得到drdList.DataSource = ds的NullReferenceException的; ? – 2011-05-24 06:17:01