Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DropDownList val1 = (DropDownList)Gridview1.Rows[intRow - 1].Cells[1].FindControl("txtItemCode");
TextBox val2 = (TextBox)Gridview1.Rows[intRow - 1].Cells[2].FindControl("txtDescription");

whats wrong here? i am getting var1 is null?
var2 is fine
Updated 29-Dec-10 2:11am

It's allright the way you're using to find control from cell.

There may the chance of dropdown absense over there or the name you have written may be slightly different then the actual control name.

You could always move on by checking if the cell has control by following way.

if (grd.Rows[0].Cells[0].HasControls())
Share this answer
The rows and cells in the grid view control are indexed as zero based. If the item code is in the first column, you need to access it at .Cells[0], not .Cells[1]. If you are getting null for the values that is the first thing I would validate.
Share this answer
Manfred Rudolf Bihy 30-Dec-10 13:27pm    
Good catch! +5
William Winner 30-Dec-10 13:30pm    
he's getting the right value for val2. And since he's specifying the specific name, then one could assume that he's using the right indices...but you know what they say about assuming!
I wouldn't go that deep into the row, try removing the Cell property.

DropDownList val1 = (DropDownList)Gridview1.Rows[intRow - 1].FindControl("txtItemCode");
TextBox val2 = (TextBox)Gridview1.Rows[intRow - 1].FindControl("txtDescription");
Share this answer
My first guess would be based on the name you gave...txtItemCode implies a textbox, not a dropdownlist.

Even if that's true, step through your code. Put a breakpoint at the line that you set val1.

Then, add a watch for Gridview1.Rows[intRow - 1].Cells[1].FindControl("txtItemCode") and see what it returns. Does it return anything? If so, is it a dropdownlist?
Share this answer
Manfred Rudolf Bihy 30-Dec-10 13:31pm    
Debugging advice, yes! +5
That seems to me one of the knowledge areas most OPs don't really have any clue about. I shall go and search if there is an article on that. If there isn't one should be created.

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