如何在不在一行中的新行中创建循环项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在不在一行中的新行中创建循环项相关的知识,希望对你有一定的参考价值。

我有一个代码将数据库中的数据显示为标签。所有数据都显示在一行中,但我想让每行数据显示在一个新行中。我正在使用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

有人可以帮我这么做吗?我会很感激。谢谢。

答案

您没有在代码中的任何位置插入新行,以便将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
另一答案
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

以上是关于如何在不在一行中的新行中创建循环项的主要内容,如果未能解决你的问题,请参考以下文章

如何使用引导程序和 for 循环在 django 中创建电影片段?

如何通过在两行之间划分特定列中的值并保持其他列不变来在熊猫数据框中创建新行?

仅基于列中的非 NaN 值在数据框中创建新行

如何使用模块化代码片段中的LeakCanary检测内存泄漏?

使用存储过程的最后一行中的单个值插入新行

如何在角度 ng-repeat 中创建新行?