我如何在datagridview中的控件上分配值 [英] How I assign a value on a control in datagridview

查看:109
本文介绍了我如何在datagridview中的控件上分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,它具有一个下拉列表和文本框.我想根据下拉列表的选定值为文本框分配一个值.但是我尝试这样得到错误对象引用未设置为对象的实例".我在这里发布我的代码(错误发生在最后两行):


I have a datagridview that has a dropdownlist and textbox. 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". I post my code here( the error is occurred on the last two line):


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;
        }




请帮忙.




Please help.

推荐答案

亲爱的达维特,

解决方案可能是这样的:-

Dear Dawit,

The solution might be like this:-

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)gvr.FindControl("txtAmount");
 
            lbl.Text = Convert.ToString(requestinfo.ElementAt(gvr.RowIndex).AmountRequested);
            
            DropDownList ddl = new DropDownList();
            ddl = (DropDownList)gvr.FindControl("ddlApprovalType");
            
            ddl.SelectedValue = "A";
            _txtAmount.Text = lbl.Text;
        }



我已经用foreach循环的[gvr]对象替换了GridID [cgvRequest]

如果有帮助,请将其标记为您的答案.

谢谢

Varun Sareen



I have replaced the GridID [cgvRequest] by [gvr] object of foreach loop

Mark this as your answer if it helps you out.

Thanks

Varun Sareen




对于那个标签,您想要将数据分配给哪个控件以及哪个控件?

首先让我澄清一下,以便我可以帮助您?
Hi ,

What for that label, to which control you want to assign the data and of which control ?

Let me clarify this first, so that i can help you ?


这篇关于我如何在datagridview中的控件上分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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