【发布时间】: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