I had the same problem as you. I found that when I use the BoundField tag in the GridView to show my data. row.Cells[1].Text works in:
GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
But when I use the TemplateField tag to show such data:
<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>
row.Cells[1].Text just returns null. I am stuck with this problem for a long time. I figure recently, and want to share with someone who has the same problem, my solution. Feel free to edit this post and / or correct me.
My decision:
Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label;
I use the Controls attribute to find the Label control that I use to display data, and you can find mine. When you find it and convert it to the correct type object, how can you extract the text and so on. Example:
string showText = lbCod.Text;
Link: Link
ๅ้ฎ ็ฒ
source share