2014-01-14 31 views
0

我試圖做一些相當簡單的事情,但由於某種原因,某些事情出錯了,我不能爲我的生活看到爲什麼。ASP.Net Gridview對於RowDataBound內的下一個循環

我有一個數據網格,RowDataBound我想循環通過另一個數據集,並顯示一個單元格內的多個值。

所以我下面的基本代碼循環訪問gridview RowDataBound中的數據集,我在循環中設置i = 0,然後我只是寫出數據集中字段的值和i的值問題是第一行在網格的每一行中寫入i = 1的值,因此它從不寫出數據集中的第一行,是否有人知道爲什麼會出現這種情況?

變量i不在其他地方使用,所以不能從某處獲取值。

Protected Sub gvReport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvReport.RowDataBound 


Dim cmdAgent As SqlCommand = New SqlCommand() 
cmdAgent.Connection = PgsSQL 
cmdAgent.CommandText = "spClientEstateAgent" 
cmdAgent.CommandType = CommandType.StoredProcedure 

Dim pCaseIdb As SqlParameter 

pCaseIdb = New SqlParameter() 
pCaseIdb.ParameterName = "@CaseId" 
pCaseIdb.SqlDbType = SqlDbType.Int 
pCaseIdb.Direction = ParameterDirection.Input 
pCaseIdb.Value = Trim(CaseId) 

cmdAgent.Parameters.Add(pCaseIdb) 

'data adapter 
Dim daAgent As SqlDataAdapter = New SqlDataAdapter 
daAgent.SelectCommand = cmdAgent 

'data set 
Dim dsAgent As DataSet = New DataSet() 
daAgent.Fill(dsAgent, "Agent") 

cmdAgent.Dispose() 
PgsSQL.Close() 

Dim AgentCount As Integer = dsAgent.Tables(0).Rows.Count 

     For i As Integer = 0 To AgentCount - 1 

      lAgents.Text = dsAgent.Tables("Agent").Rows(i).Item("Name") & " : " & i 

     Next 

End Sub 

由於提前, J.

回答

0

不用擔心,這是我失去了一些東西明目張膽的!

當我循環時,我覆蓋標籤lAgents中的前一個值,所以它只顯示最後一個增加值!

因此,我創建了一個字符串變量,並將所有值連接到字符串,然後在循環後將其應用於lAgents.text。

這是一個笨蛋,但這可能會幫助別人像我一樣愚蠢。