I think my question title is quite straight forward.
Any help is appreciated..
If it's a BoundField you could do
gv.Rows[1].Cells[1].Text;
If it's a TemplateField, you have to get the control that has the value you want.
Label L = gv.Rows[1].FindControl("yourcontrolId") as Label;
L.Text;
With BoundField and in readonly mode you can use GridView1.Rows[x].Cells[x].Text
but with edit mode you have to use Controls collection to get reference of a control. This method returns a Control
object.
Control control=GridView1.Rows[x].Cells[x].Controls[0]; // later you may cast it to appropriate control class.
If template field is used then you have to issue FindControl
method from the Cells
collection to get reference of a control based upon its ID
. You may also use Cells[x].Controls
collection too.
Control control=GridView1.Rows[x].Cells[x].FindControl("ID_Of_Control"); // later you may cast it to appropriate control class.
EDIT:
It is also possible that there can be one or more controls having same name/ID across the Templatefields. In that case you can't use FindControl
method.
Example:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now to get Button and change its text from 2nd row and 1st cell:
Button btn = GridView1.Rows[1].Cells[0].Controls[1] as Button ;
if(btn!=null)
btn.Text = "Hello";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.