一个值分配给在DataGridView控制 [英] Assign a value to a control in the datagridview

查看:99
本文介绍了一个值分配给在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 by FindControl() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆