2011-04-21 46 views
0

我有一個函數返回一個帳號列表作爲一個Arraylist。我試圖將每個帳戶作爲另一個子例程中的命令參數來獲取有關每個帳號的更多數據。這隻會返回arraylist中最後一個帳號的數據。我需要使用每個帳號,調用數據庫,獲取附加信息並將所有數據存儲到Gridview(數據綁定)中。示例:如果我的數組列表中有3個帳號,則返回3行數據到gridview。我正在努力如何獲取Arraylist中每個值(帳號)的所有信息。有人能指出我在正確的方向?我認爲這可以做到,但我不確定我的方法是否正確。也許我需要創建數據表,其中包含通過ArrayList傳遞的每個值的附加信息....任何想法?查詢數據庫中的每個對象在Arraylist和數據綁定到gridview?

@ jwatts1980感謝您的評論:我會盡力澄清。我有一個帳號數組列表(也許這是我的軌道)我試圖使用此ArrayList中的值作爲命令參數在另一個調用不同的表/文件,返回這些帳戶的更多信息。我公司將提供代碼的一部分,以幫助闡明它是什麼,我試圖做的事:

Private Function ReturnMultAccts(ByVal strAcct) As ArrayList 
Dim acctsDetail As New ArrayList 
Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString 
    Dim sql As String = "SELECT DISTINCT * FROM FILE WHERE ACCTNUM=?" 
    Using conn As New OdbcConnection(dsn) 
     Using cmd As New OdbcCommand(sql, conn) 
      conn.Open() 
      cmd.Parameters.Add("ACCTNUM", OdbcType.VarChar, 20).Value = strAcct 
      Dim rdrUsers As OdbcDataReader = cmd.ExecuteReader() 
      If rdrUsers.HasRows Then 
       While rdrUsers.Read() 
      acctsDetail.Add(Trim(rdrUsers.Item("ACCTNUM"))) 
       End While 
      End If 
      rdrUsers.Close() 
      conn.Close() 
     End Using 
    End Using 

這將返回帳號的ArrayList(可以說是3 ACCT號)。我從另一個子集叫這個函數:

Private Sub GetMoreAcctInfo(ByVal strAcct) 
    'Create New ArrayList 
    Dim MultAccts As New ArrayList 
    'Pass strAcct to Function to get Multiples 
    MultAccts = ReturnMultAccts(strAcct) 
    'Create the variable BachNum for the loop 
    Dim BachNum As String = MultAccts.Item(0) 
    For Each BachNum In MultAccts 
    'Get All of the necessary info from OtherFile based on the BachNum for BOS's 
     Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString 
     Dim sql As String = "SELECT ACCTNUM, BILSALCOD1, BILSALCOD2, BILSALCOD3, OTHACCTNUM FROM OtherFile WHERE OTHACCTNUM=?" 'Equal to the items in the arraylist 
     Using conn As New OdbcConnection(dsn) 
      Using cmd As New OdbcCommand(sql, conn) 
       conn.Open() 
       cmd.Parameters.Add("OTHACCTNUM", OdbcType.VarChar, 20).Value = BachNum 
       Using adapter = New OdbcDataAdapter(cmd) 
        Dim DS As New DataSet() 
        adapter.Fill(DS) 
        GridView1.DataSource = DS 
        GridView1.DataBind() 
       End Using 
      End Using 
     End Using 
    Next 
End Sub 

希望這能說明我正在嘗試做什麼......?

+2

您正在使用....?我認爲你的東西和你的 – 2011-04-21 19:18:50

+0

的結尾對Gridview後的錯誤時期感到抱歉...應該說「...並綁定到我正在使用的gridview」。對困惑感到抱歉。 – 2011-04-22 15:37:07

+0

我將此添加爲評論而不是答案,因爲我對此不完全確定。但是我認爲,如果你從你的函數返回一個數組的數據,然後把所有這些數組放到一個數組列表中(即對象數組的數組列表),那麼你可以將數組列表綁定爲數據源gridview。 – jwatts1980 2011-04-27 18:16:29

回答

1

爲了詳細說明我的建議,您需要一個強類型對象列表。您可以將這些項目添加到列表中,然後將列表綁定到GridView。

我會從頭開始。您知道來自數據庫的數據類型:ACCTNUM,BILSALCOD1,BILSALCOD2,BILSALCOD3和OTHACCTNUM。所以你可以使用它們來創建一個對象。

Friend Class AccountClass 
    Private pACCTNUM As string = "" 
    Private pBILSALCOD1 As string = "" 
    Private pBILSALCOD2 As string = "" 
    Private pBILSALCOD3 As string = "" 
    Private pOTHACCTNUM As string = "" 

    Public Property ACCTNUM() As string 
     Get 
      Return pACCTNUM 
     End Get 
     Set(ByVal value as string) 
      Me.pACCTNUM = value 
     End Set 
    End Property 

    Public Property BILSALCOD1() As string 
     Get 
      Return pBILSALCOD1 
     End Get 
     Set(ByVal value as string) 
      Me.pBILSALCOD1 = value 
     End Set 
    End Property 

    Public Property BILSALCOD2() As string 
     Get 
      Return pBILSALCOD2 
     End Get 
     Set(ByVal value as string) 
      Me.pBILSALCOD2 = value 
     End Set 
    End Property 

    Public Property BILSALCOD3() As string 
     Get 
      Return pBILSALCOD3 
     End Get 
     Set(ByVal value as string) 
      Me.pBILSALCOD3 = value 
     End Set 
    End Property 

    Public Property OTHACCTNUM() As string 
     Get 
      Return pOTHACCTNUM 
     End Get 
     Set(ByVal value as string) 
      Me.pOTHACCTNUM = value 
     End Set 
    End Property 

    Sub New(ByVal ACCTNUM As string, ByVal BILSALCOD1 As string, ByVal BILSALCOD2 As string, ByVal BILSALCOD3 As string, ByVal OTHACCTNUM As string) 
     Me.ACCTNUM = ACCTNUM 
     Me.BILSALCOD1 = BILSALCOD1 
     Me.BILSALCOD2 = BILSALCOD2 
     Me.BILSALCOD3 = BILSALCOD3 
     Me.OTHACCTNUM = OTHACCTNUM 
    End Sub 
End Class 

然後你重寫GetMoreAcctInfo()例程來使用它。

Private Sub GetMoreAcctInfo(ByVal strAcct) 
    'Create New ArrayList 
    Dim MultAccts As ArrayList 

    'Pass strAcct to Function to get Multiples 
    MultAccts = ReturnMultAccts(strAcct) 

    'Create the variable BachNum for the loop 
    Dim BachNum As String 

    'Create the list to bind to the grid 
    Dim AcctInfo As New Generic.List(Of AccountClass) 

    'create the dataset 
    Dim DS As DataSet 

    For Each BachNum In MultAccts 
    'Get All of the necessary info from OtherFile based on the BachNum for BOS's 
     Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString 
     Dim sql As String = "SELECT ACCTNUM, BILSALCOD1, BILSALCOD2, BILSALCOD3, OTHACCTNUM FROM OtherFile WHERE OTHACCTNUM=?" 'Equal to the items in the arraylist 
     Using conn As New OdbcConnection(dsn) 
      Using cmd As New OdbcCommand(sql, conn) 
       conn.Open() 
       cmd.Parameters.Add("OTHACCTNUM", OdbcType.VarChar, 20).Value = BachNum 
       Using adapter = New OdbcDataAdapter(cmd) 
        DS = New DataSet() 
        adapter.Fill(DS) 

        For Each t As DataTable In DS.Tables 
         For Each r As DataRow In t.Rows 
          AcctInfo.Add(new AccountClass(r("ACCTNUM"), r("BILSALCOD1"), r("BILSALCOD2"), r("BILSALCOD3"), r("OTHACCTNUM"))) 
         Next 
        Next 
       End Using 
      End Using 
     End Using 
    Next 

    GridView1.DataSource = AcctInfo 
    GridView1.DataBind() 
End Sub 
+0

jwatts1980感謝您的幫助(特別是添加的代碼)我一定會在早上第一次嘗試這個。如果我遇到任何「困難」,我一定會提出更多問題。謝謝, – 2011-04-28 19:02:47

+0

由於某種原因,我無法得到這個工作。我今天將繼續努力,看看我能否實現它。我想我可能會遇到函數ReturnMultAccts的問題。我可以將此函數中的數據顯示/數據綁定到Gridview,以查看Sub GetMoreAcctInfo工作所需的OTHACCTNUM。但它似乎並不像我認爲應該那樣循環訪問數據。 – 2011-04-29 19:33:19

+0

我終於得到了這個工作。它現在在gridview中顯示數據。我對Gridview中的數據有疑問。有沒有辦法改變列標題?有沒有辦法將特定的數據行傳遞給另一個子例程(比如RowDataBound?)。說上校(1)或上校(2)? – 2011-05-02 17:09:31