GridView中的行数据绑定 [英] Row Data Bound in GridView
问题描述
海,
我在编辑项目模板的gridview内有一个下拉列表.我已将详细信息绑定到行数据绑定事件中.这是我的代码:
Hai,
I have a dropdownlist inside gridview in edit item template.I have binded the details in row data bound event.Here is my code:
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//DropDownList ddlyear = (DropDownList)e.Row.FindControl("DropDownList4");
DropDownList ddlyear = (DropDownList)e.Row.FindControl("DropDownList4");
int year = System.DateTime.Now.Year;
for (int intCount = 1900; intCount <= year; intCount++)
{
ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));
}
}
}
catch (Exception ex)
{
spanid.InnerHtml = ex.Message;
}
}
我在此行" ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));"
中遇到错误.错误是对象引用未设置为对象的实例"..CAn任何人都请告诉我上面代码中的错误是什么.在单个Edit item模板中,我保留了2个下拉列表和1个下拉列表textbox.感谢Advance
Iam getting error in this line " ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));"
. Error is "Object Reference not set to an instance of the object".CAn anyone pls tell me what is the error in the above code.In a single Edit item template,I have kept 2 dropdown and one textbox.Thanks in Advance
推荐答案
对象引用未设置为对象的实例
当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
由于intCount已初始化,因此不能为null.唯一原因看起来像ddlyear
对象必须是NULL
.这意味着此处的行中没有"DropdownList4".检查并解决.
Object reference not set to an instance of an object
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]
A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
For now, based on what you say "Iam getting error in this line " ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));"."
Since intCount is initialized, it cannot be null. Only reason looks likeddlyear
object must beNULL
. This means, there was no ''DropdownList4'' in the row here. Check it and resolve.
这篇关于GridView中的行数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!