0
A
回答
2
随着绑定列和只读模式下可以使用GridView1.Rows[x].Cells[x].Text
但编辑模式下,您必须使用控件集合得到控制的参考。此方法返回一个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 btn = GridView1.Rows[1].Cells[0].Controls[1] as Button ;
if(btn!=null)
btn.Text = "Hello";
3
如果它是一个BoundField,你可以做
gv.Rows[1].Cells[1].Text;
如果它是一个TemplateField,你必须得到有你想要的值控制。
Label L = gv.Rows[1].FindControl("yourcontrolId") as Label;
L.Text;
相关问题
- 1. 获取来自81元素索引数组的行和列值
- 2. 获得具有给定行索引和列索引的datagrid单元格的值
- 3. 通过字符串而不是索引获取Gridview单元格的列引用
- 4. GridView单元格的单元格索引
- 5. 获取GridView中选定行的索引
- 6. C#DataTable,按行/列索引获取值
- 7. 如何获得对数组中单元格的引用,只知道它的值?
- 8. 如何获取Datagrid行索引以获取上一行的单元格值
- 9. 如何在gridview中获取单元格索引
- 10. 如何按C#中Datatable的单元格值获取行索引
- 11. 获取索引给定的行和列
- 12. Gridview丢失行索引值
- 13. 从ASP.NET GridView获取值单元格
- 14. 从特定的Gridview单元获取值
- 15. 从Gridview单元获取价值
- 16. 如何从gridview获取单元格值?
- 17. 在Gridview中获取单元格的值
- 18. 如果我知道单元格的行和列索引,如何更改HTML表格单元格中的属性?
- 19. 我想在gridview中的gridview中获取选定索引的值
- 20. 从tableviewcell获取单元格索引
- 21. 从选定的行gridview获取单元格值
- 22. Gridview,行更新获取单元格中的值
- 23. 获取gridview中点击列的列索引
- 24. 如何获取按列名称的单元格值而不是通过在asp.net中的GridView中的索引
- 25. 获取行索引
- 26. 按索引获取列表值
- 27. 在引导表中单击时获取行的单元格值
- 28. 如何使用列和行索引值设置datagrid单元格的值?
- 29. 如何从索引路径获取单元格值? - IOS
- 30. 如何获取用户点击javascript的gridview行的行索引?
嘿,thx,但我实际上应用此代码之前,它不工作...我使用模板字段fyi ...任何替代? – rofans91 2011-12-27 02:18:54
@ Rofans.NET - 有两种方法可以从TemplateField单元格获取控件 - FindControl方法和Controls集合。 – adatapost 2011-12-27 02:24:48
@AVD:'FindControl'找到控件而不会提取单元格索引,对吧? – naveen 2011-12-27 02:28:37