我的gridview有一个问题,我每次循环时都会追加到字符串的末尾,我尝试在再次循环之前删除附加(如下面的代码所示)但它似乎没有起作用:
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多次,我不希望这个....我希望这是有道理的。
无论如何,我对如何解决这个问题感到困惑,Google对此毫无帮助,我们将不胜感激。
答案 0 :(得分:1)
很多时候它会附加很多次,因为你在克隆中循环遍历行很多次。
将tblAccounts
行添加到dtClone
之后,您遍历dtClone
中的所有行,然后对新行及其前面的所有行执行操作,因此它是正常,你要多次附加字符串结束。
为什么不从tblAccounts
克隆行,处理它然后将其添加到dtClone
?