从数据集中填充 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;
行
我该如何解决这个问题???
I'm using the code below:
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();
}
}
It gives an error as:
Object reference not set to an instance of an object.
At the line drdList.DataSource = ds;
How do I fix this???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在以下代码行中指定 COLUMN:
另一种选择是循环遍历另一个
foreach
中的列根据您的评论提供更多信息:
看起来您是 ASP.NET 新手,这就是我推荐的原因以下内容:
使用 asp:TemplateColumn 并将 asp:DropDownList 放入 EditTemplate 中。将 DropDown 连接到 SqlDataSource(或您想要使用的任何其他数据源)。
绑定将为您处理。
如果没有看到您的 ASP.NET 代码并了解有关您的需求的更多信息,我无法进一步详细说明。
Try specifying the COLUMN in the following line of code:
Another option is to loop through columns in another
foreach
More info based on your comment:
Looks like you're new to ASP.NET, that's why I recommend the following:
Use asp:TemplateColumn and put the asp:DropDownList in the EditTemplate. Hook the DropDown up to SqlDataSource (or whatever else datasource you want to use).
The binding will be handled for you.
I can't elaborate any further w/o seeing your ASP.NET code and knowing more about your requirements.
试试这个
Try This