(TextBox)
That's a "type boxing", means forcing to convert to a specific data type, see Boxing and Unboxing (C# Programming Guide)
Controls[0]
That's simply accessing the first (zero-based) element of an array, see Array Class
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
GridViewRow changedRow = GridView1.Rows[e.RowIndex];
string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;
I don't understand what are (TextBox)
and Controls[0]?
What do they do? What are they used for?
(TextBox)
That's a "type boxing", means forcing to convert to a specific data type, see Boxing and Unboxing (C# Programming Guide)
Controls[0]
That's simply accessing the first (zero-based) element of an array, see Array Class
(TextBox) is a cast (not an box/unbox).
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions
as you might guess Cells is an army of cells of a grid row. .Controls is a collection of Controls in the cell. the author assumes the first control in the second cell is a TextBox (which inherits from Control). The cast allows accessing unique TextBox properties like .Text. if the control is not a TextBox, the cast will throw a runtime error.
box/unbox is for converting value types to objects and back. most commonly used for numeric types. this is a feature f most typed languages. you either pass the actual value via the stack, or you pass a value pointer allocated on the heap.
Hi @MikeR
First of all
GridViewRow changedRow = GridView1.Rows[e.RowIndex];
Declare a variable of type GridViewRow whose value is the "e.RowIndex+1" row of GridView1.
string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;
Declare a string variable firstname. Strongly convert this GridViewRow variable to a TextBox variable via '(TextBox)changedRow'. (Provided you can convert this control to a TextBox control.)
‘Cells[1]. Controls[0]' represents the first Control of the second Row.
ForExample
Code:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="D1">
<ItemTemplate>
<asp:TextBox ID="D1" runat="server" Text='Test1'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D2">
<ItemTemplate>
<asp:TextBox ID="D2" runat="server" Text='Test2'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
GridViewRow a = GridView1.Rows[0];
string firstname = ((TextBox)a.Cells[0].Controls[1]).Text;
TextBox1.Text = firstname;
Result:
Best regards,
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.