I had the same problem as yours. I found that when i use the BoundField
tag in GridView to show my data. The row.Cells[1].Text
is working in:
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
But when i use TemplateField
tag to show data like this:
<asp:TemplateField HeaderText="??">
<ItemTemplate>
<asp:Label ID="Part_No" runat="server" Text='<%# Eval("Part_No")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
The row.Cells[1].Text
just return null. I got stuck in this problem for a long time. I figur out recently and i want to share with someone who have the same problem my solution. Please feel free to edit this post and/or correct me.
My Solution:
Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label;
I use Controls
attribute to find the Label
control which i use to show data, and you can find yours. When you find it and convert to the correct type object than you can extract text and so on. Ex:
string showText = lbCod.Text;
Reference: reference