一个值分配给在DataGridView控制 [英] Assign a value to a control in the datagridview
问题描述
我想这取决于DropDownList的选定值赋值给一个文本框。不过,我试试这个得到的错误
对象引用不设置到对象的实例
块引用>下面是我的code:
的foreach(在cgvRequest.Rows GridViewRow GVR)
{
//编程方式从模板列访问复选框 // CheckBoxField字段CHK =(CheckBoxField字段)gvr.FindControl(chkMark);
标签LBL =(标签)gvr.FindControl(lblRequestAmount); 文本框_txtAmount =(文本框)cgvRequest.FindControl(txtAmount); lbl.Text = Convert.ToString(
requestinfo.ElementAt(gvr.RowIndex).AmountRequested); DropDownList的DDL =新的DropDownList();
DDL =(DropDownList的)cgvRequest.FindControl(ddlApprovalType); ddl.SelectedValue =A;
_txtAmount.Text = lbl.Text;
}
解决方案这是你所得到的原因
对象引用不设置到对象的实例
块引用>是控件(DropDownList的和/或文本框)你想设置一个属性来为
空
。这意味着它无法通过的FindControl找到()
方法。我只是假设这是因为我不能调试您的code,但我敢打赌,那是因为你正在调用
的FindControl
上的数据网格,而不是行试着改变你的code为:
文本框_txtAmount =(文本框)gvr.FindControl(txtAmount);
和
DropDownList的DDL =新的DropDownList();
DDL =(DropDownList的)gvr.FindControl(ddlApprovalType);I want to assign a value to a textbox depending on the selected value of the dropdownlist. But I try this getting the error
"Object reference not set to an instance of an object"
Here is my code:
foreach (GridViewRow gvr in cgvRequest.Rows) { //Programmatically access the CheckBox from the TemplateField //CheckBoxField chk = (CheckBoxField)gvr.FindControl("chkMark"); Label lbl = (Label)gvr.FindControl("lblRequestAmount"); TextBox _txtAmount = (TextBox)cgvRequest.FindControl("txtAmount"); lbl.Text = Convert.ToString( requestinfo.ElementAt(gvr.RowIndex).AmountRequested); DropDownList ddl = new DropDownList(); ddl = (DropDownList)cgvRequest.FindControl("ddlApprovalType"); ddl.SelectedValue = "A"; _txtAmount.Text = lbl.Text; }
解决方案The reason that you are getting
"Object reference not set to an instance of an object"
is that the control (dropdownlist and/or textbox) you are trying to set a property to is
null
. Which means it cannot be found byFindControl()
method.I am just assuming this because I cannot debug your code, but I bet it is because you are calling
FindControl
on the data grid and not on the row.Try changing your code to:
TextBox _txtAmount = (TextBox)gvr.FindControl("txtAmount");
and
DropDownList ddl = new DropDownList(); ddl = (DropDownList)gvr.FindControl("ddlApprovalType");
这篇关于一个值分配给在DataGridView控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!