String.Join不使用vb​​CrLf

时间:2013-02-12 13:36:41

标签: asp.net vb.net webforms

我有以下程序,工作正常。我遇到问题的唯一部分是 CompNames列表有多于1条记录。我试图将 String.Join 与vbCrLf一起使用,但它不起作用。

任何人都有任何想法或我可以使用的替代方案。

Public Sub gvTeamList_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim TeamID As Integer

    If e.Row.RowType = DataControlRowType.DataRow Then
        TeamID = DataBinder.Eval(e.Row.DataItem, "TeamID")
        Dim sSQL As String
        sSQL = "SELECT C.CompetitionName, CTT.TeamID " & _
                "FROM tblCompetition C " & _
                "left join  tblCompetitionToTeam CTT on C.CompetitionID = CTT.CompetitionID " & _
                "left join  tblTeam T on CTT.TeamID = T.TeamID " & _
                "where CTT.TeamID = " & TeamID
        Dim dr = DataClass.GetDataReader(sSQL)
        Dim bRows As Boolean = dr.HasRows
        Dim CompNames As New List(Of String)
        While dr.Read
            CompNames.Add(dr("CompetitionName"))
        End While
        Dim Name As String
        If CompNames.Count > 0 Then
            For Each Name In CompNames
                e.Row.Cells(5).Text = String.Join(vbCrLf, CompNames.ToArray)
            Next
        End If
        'e.Row.Cells(5).Text = 
        e.Row.Cells(5).ForeColor = Drawing.Color.Yellow
        e.Row.Cells(5).BackColor = Drawing.Color.DarkBlue
        dr.Close()
    End If

End Sub

我也尝试过 Environment.NewLine 并且无法正常工作

1 个答案:

答案 0 :(得分:2)

您似乎正在使用WebForms应用程序。在HTML中,换行通常没有效果,因为忽略了空格(除非它嵌入在某些标签中)。您想使用<br />生成换行符:

e.Row.Cells(5).Text = String.Join("<br />", CompNames.ToArray)

此外,您不需要For Each循环,因为String.Join在一次调用中枚举整个数组。对CompNames中的每个名称运行一次是多余的。