ASP.NET GridView按名称访问单元格

时间:2009-10-07 02:35:42

标签: asp.net

我希望按名称而不是索引访问数据网格中某个单元格,以防列重新排序(已经发生了这种情况)

MyBox.Text = ListAllCategories.Rows [ListAllCategories.SelectedIndex] .Cells [1] .Text;

我没有使用“Cells [1]”,而是想做类似“Cells [”Name“]”(这不起作用)或类似的东西,其中“Name”是GridView中列的名称

谁能告诉我怎么做?

1 个答案:

答案 0 :(得分:0)

创建更多列时,请勿使用失败的索引。

而是使用MyGrid.FindControl(“NameOfYourControl”)。

例如假设有一个名为lblID的标签,一个名为gvMyGridView的网格视图。

试试这个:

Dim l as Label
l = Me.gvMyGridView.FindControl("lblID")
You can also use CType to cast it or get the text contents of this label, etc.