Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, now at school we are learning how to use GridView. We have a table with all the information of all the users. When we want to update a row, we need to store name, last name and other data in separate variables. Our teacher does it this way:

C#
GridViewRow changedRow = GridView1.Rows[e.RowIndex];
string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;


What I have tried:

I don't understand what are (TextBox) and Controls[0]? What do they do? What are they used for?
Posted
Updated 8-Mar-23 0:03am

1 solution

C#
GridViewRow changedRow = GridView1.Rows[e.RowIndex];
string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;

changedRow : select the row in GridView1 at the offset in e.RowIndex
Select the control at changedRow.Cells[1].Controls[0]. That is select the second cell in the row item. That cell (I assume) contains a number of controls, so you are selecting the first one (offsets start at zero). You then tell the compiler to assume that the resulting object is a TextBox type. This is known as casting and will only work if the control at that point actually is a TextBox or something derived from it.

But these are basic C# concepts which you should have been taught first. I suggest you have a chat with your teacher about this.
 
Share this answer
 
Comments
Mike Radunski 8-Mar-23 6:07am    
Thank you very much
Richard MacCutchan 8-Mar-23 6:22am    
You are welcome.

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