【问题标题】:Strings not displaying correctly in Gridview字符串在 Gridview 中未正确显示
【发布时间】:2014-07-16 00:12:28
【问题描述】:

我的 gridview 有问题,我每次通过我的 for each 循环都附加到字符串的末尾,我尝试在它再次通过循环之前删除附加(如下面的代码所示)但它似乎不起作用:

Private Sub LoadData()

    conn = New SqlConnection(connectionString)
    ds = New DataSet("Accounts")
    da = New SqlDataAdapter("SELECT [branch], [no] , [surname], [name], [type], [sub], [totalAmount], loc, locstatus, HoldCalc, odTimes " & _
                            "FROM [DmdOD]", conn)

    conn.Open()

    da.FillSchema(ds, SchemaType.Source, "Accounts")
    da.Fill(ds, "Accounts")

    conn.Close()

    Dim loc As String
    Dim hold As String
    Dim holdString As String
    Dim odString As String

    Dim tblAccounts As DataTable
    tblAccounts = ds.Tables("Accounts")

    'Clone table in order to manipulate data
    Dim dtClone As DataTable = tblAccounts.Clone()
    dtClone.Columns("loc").DataType = System.Type.GetType("System.String")
    dtClone.Columns("odtimes").DataType = System.Type.GetType("System.String")
    dtClone.Columns("HoldCalc").DataType = System.Type.GetType("System.String")

    'Perform logic on fields before binding to gridview

    tblAccounts = DeleteDuplicateFromDataTable(tblAccounts, "no")

    For Each dr As DataRow In tblAccounts.Rows
        dtClone.ImportRow(dr)
        For Each drClone As DataRow In dtClone.Rows
            loc = drClone.Item("loc")
            odString = drClone.Item("odtimes")
            hold = drClone.Item("HoldCalc")
            If loc = "0.0000" Then
                loc = " "
                drClone.Item("loc") = loc
            End If

            'hold = CType(drClone.Item("HoldCalc"), Decimal)
            holdString = hold

            If loc <> "0.0000" AndAlso holdString < "0.0000" Then
                holdString &= " EX"
                drClone.Item("HoldCalc") = holdString
            ElseIf loc = "0.000" AndAlso hold < 0 Then
                holdString &= " OD"
                drClone.Item("HoldCalc") = holdString
            Else
                holdString = ""
                drClone.Item("HoldCalc") = holdString
            End If

            If odString = "0" Then
                odString = ""
                drClone.Item("odtimes") = odString
            End If

            If holdString.Length > 2 AndAlso (holdString.Contains(" EX EX") OrElse holdString.Contains(" OD OD")) Then
                drClone.Item("HoldCalc") = holdString.Substring(0, holdString.Length - 2)
            End If
        Next
    Next



    dtClone.AcceptChanges()


    GVAccounts.DataSource = dtClone
    GVAccounts.DataBind()

End Sub

每次通过循环时,它都会附加到字符串的末尾,尽管如果满足条件,它会进入我的 If 语句,但我仍然会得到不正确的输出。我想要的输出应该只在价格之后有 EX 或 OD,但它在最后多次输出 EX,我不想要这个....我希望这是有道理的。

无论如何,我对如何解决这个问题感到困惑,而谷歌对此没有任何帮助,我们将不胜感激。

【问题讨论】:

    标签: asp.net vb.net gridview datatable


    【解决方案1】:

    它会追加很多次是很有意义的,因为您在 clone 中的行中循环了很多次。

    tblAccounts 中的行添加到dtClone 之后,您将遍历dtClone 中的所有行并对新行及其之前的所有行执行操作,因此您以字符串被多次附加。

    您为什么不直接从tblAccounts 克隆行,对其进行处理,然后将其添加到dtClone

    【讨论】:

    • 我需要更改行的数据类型才能处理它,这就是我在那里所做的。由于该行已经在 tblAccounts 中建立,我必须克隆它才能更改值。
    • 或者根据我的理解。
    • 是的,这很好,但是您会多次循环遍历 'dtClone' 中的所有行,这不是必需的。每次你添加“EX”,这就是为什么你最终会得到错误的格式。
    • 我只是不确定如何按照您的建议去做,如果可以的话,您认为您可以提供一个代码示例吗?
    • 只需取出第一个循环并执行dtClone = tblAccounts.Clone 之类的操作,然后遍历dtClone 的行并进行处理。它也应该快得多。或者,不要循环遍历dtClone,而是执行clonedRow = dr.Clone(),处理然后添加到dtClone
    猜你喜欢
    • 2019-03-20
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2012-08-08
    相关资源
    最近更新 更多