Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):


C#
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.
Posted
Updated 25-Dec-11 23:40pm
v2
Comments
Karthik Harve 26-Dec-11 5:40am    
[Edit] pre tags added.

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


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
 
Share this answer
 
Comments
Dawit Ab 26-Dec-11 6:24am    
Thank you it works.
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 ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900