如何在新行而不是一行中制作循环项

时间:2018-12-19 04:06:22

标签: vb.net

我有一个代码可以将数据库中的数据显示到标签中。所有数据都显示在一行中,但我想使每一行数据显示在新行中。 我正在使用VB.Net

Aspx.vb

Sub ResourceName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
  Dim LanguageName As String = vbNullString
  Dim LangNameList As String = vbNullString
  Dim LangWrittenList As String = vbNullString
  Dim LangSpokenList As String = vbNullString
  Dim WrittenRate As String = vbNullString
  Dim SpokenRate As String = vbNullString


  lblLanguageName.Text = ""
  lblLanguageWritten.Text = ""
  lblLanguageSpoken.Text = ""

   attPage.SQLQuery = DC.Data_TechnicalResource("12",chkResourceName1.SelectedValue)

   DS = DA.GetSQLDataset(attPage.SQLQuery)

        If DS IsNot Nothing AndAlso DS.Tables(0).Rows.Count > 0 Then
          For Each dr In DS.Tables(0).Rows
                LanguageName = dr("LanguageName").ToString
                WrittenRate = dr("WrittenLevel").ToString
                SpokenRate = dr("SpokenLevel").ToString
                If cnt <> 0 Then
                    LangNameList = LangNameList + LanguageName
                    LangWrittenList = LangWrittenList + WrittenRate
                    LangSpokenList = LangSpokenList + SpokenRate
                Else
                    LangNameList = LanguageName
                    LangWrittenList = WrittenRate
                    LangSpokenList = SpokenRate
                End If
                cnt = cnt + 1
            Next

        End If
        lblLanguageName.Text = LangNameList
        lblLanguageWritten.Text = LangWrittenList
        lblLanguageSpoken.Text = LangSpokenList

输出如下

Bahasa MalaysiaEnglishTamil          10,10,4     10,10,4

我希望输出应该是这样

Bahasa Malaysia          10           10

English                  10           10

Tamil                    4             4

任何人都可以帮助我做到这一点吗?我会很感激。谢谢。

2 个答案:

答案 0 :(得分:0)

您不会在代码中的任何位置插入新行,以将if块代码更改为此。

            If cnt <> 0 Then
                LangNameList = LangNameList + vbNewLine + LanguageName
                LangWrittenList = LangWrittenList + vbNewLine + WrittenRate
                LangSpokenList = LangSpokenList + vbNewLine + SpokenRate
            Else
                LangNameList = LanguageName
                LangWrittenList = WrittenRate
                LangSpokenList = SpokenRate
            End If

因此,您的整个代码应如下所示:

Sub ResourceName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
  Dim LanguageName As String = vbNullString
  Dim LangNameList As String = vbNullString
  Dim LangWrittenList As String = vbNullString
  Dim LangSpokenList As String = vbNullString
  Dim WrittenRate As String = vbNullString
  Dim SpokenRate As String = vbNullString


  lblLanguageName.Text = ""
  lblLanguageWritten.Text = ""
  lblLanguageSpoken.Text = ""

   attPage.SQLQuery = DC.Data_TechnicalResource("12",chkResourceName1.SelectedValue)

   DS = DA.GetSQLDataset(attPage.SQLQuery)

    If DS IsNot Nothing AndAlso DS.Tables(0).Rows.Count > 0 Then
      For Each dr In DS.Tables(0).Rows
            LanguageName = dr("LanguageName").ToString
            WrittenRate = dr("WrittenLevel").ToString
            SpokenRate = dr("SpokenLevel").ToString
            If cnt <> 0 Then
                LangNameList = LangNameList + vbNewLine + LanguageName
                LangWrittenList = LangWrittenList + vbNewLine + WrittenRate
                LangSpokenList = LangSpokenList + vbNewLine + SpokenRate
            Else
                LangNameList = LanguageName
                LangWrittenList = WrittenRate
                LangSpokenList = SpokenRate
            End If
            cnt = cnt + 1
        Next

    End If
    lblLanguageName.Text = LangNameList
    lblLanguageWritten.Text = LangWrittenList
    lblLanguageSpoken.Text = LangSpokenList

答案 1 :(得分:0)

Sub ResourceName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
  Dim LanguageName As String = vbNullString
  Dim LangNameList As String = vbNullString
  Dim LangWrittenList As String = vbNullString
  Dim LangSpokenList As String = vbNullString
  Dim WrittenRate As String = vbNullString
  Dim SpokenRate As String = vbNullString


  lblLanguageName.Text = ""
  lblLanguageWritten.Text = ""
  lblLanguageSpoken.Text = ""

   attPage.SQLQuery = DC.Data_TechnicalResource("12",chkResourceName1.SelectedValue)

   DS = DA.GetSQLDataset(attPage.SQLQuery)

        If DS IsNot Nothing AndAlso DS.Tables(0).Rows.Count > 0 Then
          For Each dr In DS.Tables(0).Rows
                LanguageName = dr("LanguageName").ToString
                WrittenRate = dr("WrittenLevel").ToString
                SpokenRate = dr("SpokenLevel").ToString
                If cnt <> 0 Then
                    LangNameList = LangNameList + LanguageName
                    LangWrittenList = LangWrittenList + WrittenRate
                    LangSpokenList = LangSpokenList + SpokenRate
                Else
                    LangNameList = LanguageName
                    LangWrittenList = WrittenRate
                    LangSpokenList = SpokenRate
                End If
                cnt = cnt + 1
            Next

        End If
        lblLanguageName.Text = LangNameList
        lblLanguageWritten.Text = LangWrittenList
        lblLanguageSpoken.Text = LangSpokenList

您要将这些值附加到变量 LangNameList

If cnt <> 0 Then
                        LangNameList = LangNameList + LanguageName
                        LangWrittenList = LangWrittenList + WrittenRate
                        LangSpokenList = LangSpokenList + SpokenRate

在此步骤中,您将附加心脏瓣膜。 表示

Ds表包含一个值列表,并且您已将表列 dr(“ LanguageName”)中的值分配给 LanguageName LanguageName = dr(“ LanguageName”)。ToString

 If cnt <> 0 Then

不要附加值

尝试打印

  For Each dr In DS.Tables(0).Rows
                    LanguageName = dr("LanguageName").ToString
                    WrittenRate = dr("WrittenLevel").ToString
                    SpokenRate = dr("SpokenLevel").ToString
                    If cnt <> 0 Then
LangNameList = LangNameList + vbNewLine + LanguageName
            LangWrittenList = LangWrittenList + vbNewLine + WrittenRate
            LangSpokenList = LangSpokenList + vbNewLine + SpokenRate
                    Else
                        LangNameList = LanguageName
                        LangWrittenList = WrittenRate
                        LangSpokenList = SpokenRate
                    End If
                    cnt = cnt + 1
                Next