[英]Get GridView Cell Value Knowing Row And Column Index Only
我认为我的问题标题很简单。
任何帮助表示赞赏。
如果是BoundField,则可以执行
gv.Rows[1].Cells[1].Text;
如果是TemplateField,则必须获取具有所需值的控件。
Label L = gv.Rows[1].FindControl("yourcontrolId") as Label;
L.Text;
在BoundField和只读模式下,可以使用GridView1.Rows[x].Cells[x].Text
但在编辑模式下,必须使用Controls集合来获取控件的引用。 此方法返回一个Control
对象。
Control control=GridView1.Rows[x].Cells[x].Controls[0]; // later you may cast it to appropriate control class.
如果使用模板字段,则必须从Cells
集合中发出FindControl
方法,以根据控件的ID
获取控件的引用。 您也可以使用Cells[x].Controls
集合。
Control control=GridView1.Rows[x].Cells[x].FindControl("ID_Of_Control"); // later you may cast it to appropriate control class.
编辑:
在模板字段中也可能存在一个或多个具有相同名称/ ID的控件。 在这种情况下,您将无法使用FindControl
方法。
例:
<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>
现在获取Button并从第二行和第一单元格更改其文本:
Button btn = GridView1.Rows[1].Cells[0].Controls[1] as Button ;
if(btn!=null)
btn.Text = "Hello";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.