1

我有以下程序,效果很好。我遇到的唯一问题是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但这也不起作用

4

1 回答 1

2

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

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

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

于 2013-02-12T13:40:32.953 回答