0

我正在尝试将templateFields我的 gridview 的值添加到List(Of String). 这些templateFields由 2 组成Labels。我想要实现的是

  1. 为了能够遍历 的每一行GridView,获取重复的 templateField 值。

  2. 将行的TemplateFields1值存储在字符串中。

  3. 将该字符串添加到字符串列表中。

这是我的代码:

For Each row As GridViewRow In GridView1.Rows`
    Dim stri As String
    Dim ttt As Label = DirectCast(row.FindControl("Title"), Label)
    Dim desc As Label = DirectCast(row.FindControl("lblDescription"), Label)
    stri = ttt.Text.ToString & desc.Text.ToString
    Dim list As New List(Of String)
    While (stri <> Nothing) 
        list.Add(stri)  ' Add every new string in a new index of the list 
    End While
Next

因此,在while循环中,将stri其存储在list(0)然后继续for loop,获取下一个并将其存储在新的同时保持stri索引中,依此类推,直到所有完成。list(1)list(0)templateField stri

有什么想法或建议吗?

4

1 回答 1

1

您必须在循环外创建列表。我还将用语句替换While循环:If

Dim list As New List(Of String)

For Each row As GridViewRow In GridView1.Rows
    Dim ttt As Label = DirectCast(row.FindControl("Title"), Label)
    Dim desc As Label = DirectCast(row.FindControl("lblDescription"), Label)
    Dim stri As String = ttt.Text.ToString & desc.Text.ToString

    If (Not String.IsNullOrEmpty(stri)) 
        list.Add(stri)
    End If
Next
于 2013-01-15T13:59:13.640 回答