2016-09-26 5 views
0

SQL Server上のテーブルからデータを取得し、データを変数に代入し、そのデータをリストビューの列の下に出力するクラスがあります、問題は私は私のSQLテーブルの最初の行を取得している私はいくつかのコンテナをデータの完全なを埋めるためにループを必要としているとlistviewにすべての行を渡す。どのように私はこれを達成するだろう、私のプログラミングの経験は非常に限られています。私はまず、以下のコードからSQLから使用可能なコンテナにデータを取得しますか?SQLからテーブルにデータを追加してそのテーブルをリストビューで使用する方法

Public Class Inventory 
Public mFirstName As String 
Public mLastName As String 
Public mComputerType As String 
Public mAssetTag As String 
Public mCheckOutDate As Date 
Public mCheckInDate As Date 
Public mExpectedReturnDate As Date 
Public mUserEmailAddress As String 
Public mLoanSubmitter As String 
Public mDeployed As Integer 

Public Sub New() 
    LoadData() 
End Sub 

Public Sub New(dr As DataRow) 

End Sub 

Private Sub LoadData() 
    Dim dbConn As HUG.Core.Database.SQLConnection 
    Dim sql As String = "" 
    Dim ds As New DataSet 

    sql = "SELECT * FROM HDData.dbo.TravelLaptopRecords " 

    dbConn = New HUG.Core.Database.SQLConnection("WorkFiles") 
    ds = dbConn.FillDataSet(sql) 

    If Not IsNothing(ds) Then 
     If ds.Tables(0).Rows.Count > 0 Then 
      With ds.Tables(0).Rows(0) 
       mFirstName = CStr(.Item("FirstName")) 
       mLastName = CStr(.Item("LastName")) 
       mComputerType = CStr(.Item("ComputerType")) 
       mAssetTag = CStr(.Item("AssetTag")) 
       mCheckOutDate = CDate(.Item("CheckOutDate")) 
       mCheckInDate = CDate(.Item("CheckInDate")) 
       mExpectedReturnDate = CDate(.Item("ExpectedReturnDate")) 
       mUserEmailAddress = CStr(.Item("UserEmailAddress")) 
       mLoanSubmitter = CStr(.Item("LoanSubmitter")) 
       mDeployed = CInt(.Item("Deployed")) 
      End With 
     End If 
    End If 

End Sub 
End Class 

これは、のみ(0)(0).Rows ds.Tablesで最初の行を確認フォームページ

Public Class Form1 
Private mLaptopInventory As Inventory 
Private isLoad As Boolean 
Private mFirstName As String 
Private mLastName As String 
Private mComputerType As String 
Private mAssetTag As String 
Private mCheckOutDate As Date 
Private mCheckInDate As Date 
Private mExpectedReturnDate As Date 
Private mUserEmailAddress As String 
Private mLoanSubmitter As String 
Private mDeployed As Integer 

Public Sub New() 

    ' This call is required by the designer. 
    InitializeComponent() 

    ' Add any initialization after the InitializeComponent() call. 
    HUG.Core.Globals.BootStrap("G:\Programs\somefile.ini") 

    isLoad = True 
End Sub 

Private Sub Form1_Shown(Sender As Object, e As EventArgs) Handles Me.Shown 
    mLaptopInventory = New Inventory() 
    LoadForm() 
    isLoad = False 

End Sub 

Private Sub LoadForm() 
    GroupBox1.Text = "Travel Laptop Inquiry" 
    InventoryList.View = View.Details 

    mFirstName = mLaptopInventory.mFirstName 
    mLastName = mLaptopInventory.mLastName 
    mComputerType = mLaptopInventory.mComputerType 
    mAssetTag = mLaptopInventory.mAssetTag 
    mCheckOutDate = mLaptopInventory.mCheckOutDate 
    mCheckInDate = mLaptopInventory.mCheckInDate 
    mExpectedReturnDate = mLaptopInventory.mExpectedReturnDate 
    mUserEmailAddress = mLaptopInventory.mUserEmailAddress 
    mLoanSubmitter = mLaptopInventory.mLoanSubmitter 
    mDeployed = mLaptopInventory.mDeployed 


    InventoryList.Items.Add(mFirstName) 
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mLastName) 
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mComputerType) 
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mAssetTag) 
    If mDeployed = -1 Then 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mCheckOutDate) 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("Item Not Returned").ToString() 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mExpectedReturnDate) 
    Else 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("Item is on Hand").ToString() 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mCheckInDate) 
     InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("").ToString() 
    End If 
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mUserEmailAddress) 
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mLoanSubmitter) 




End Sub 
End Class 
+0

?通常は、 'ListView'の' DataSource'プロパティとして 'DataTable'を設定します。明示的には、結果の最初のレコードの値に変数の束を設定していますが、ここではすべて「ListView」を使用していません。 – David

+2

Windowsフォームを使用していると仮定すると、単に 'DataTable'を' DataGridView'の 'DataSource'プロパティに代入するだけです。 'ListView'はグリッドコントロールではありませんので、一つとして使うべきではありません。あなたは何もないコードをたくさん書いています。これは、グリッドを埋めるための1つのライナーです。 – jmcilhinney

+0

DataTable *は型指定されたデータを保持する* "使用可能なコンテナ"です – Plutonix

答えて

1

あります。すべての行をループし、リストにデータを追加する必要があります。

Public Sub New() 
End Sub 

は、その後、あなたがInventory.LoadData()を呼び出したときに、少なくとも今、あなたはすべて取得します公共の共有

Public Shared Function LoadData() As List(Of Inventory) 
    Dim dbConn As HUG.Core.Database.SQLConnection 
    Dim sql As String = "" 
    Dim ds As New DataSet 
    Dim result As New List(Of Inventory) 

    sql = "SELECT * FROM HDData.dbo.TravelLaptopRecords " 

    dbConn = New HUG.Core.Database.SQLConnection("WorkFiles") 
    ds = dbConn.FillDataSet(sql) 

    If Not IsNothing(ds) Then 
     For i As Integer = 0 To ds.Tables(0).Rows.Count-1 
      Dim newInventory As New Inventory 

      newInventory.mFirstName = CStr(ds.Tables(0).Rows(i).Item("FirstName")) 
      newInventory.mLastName = CStr(ds.Tables(0).Rows(i).Item("LastName")) 
      newInventory.mComputerType = CStr(ds.Tables(0).Rows(i).Item("ComputerType")) 
      newInventory.mAssetTag = CStr(ds.Tables(0).Rows(i).Item("AssetTag")) 
      newInventory.mCheckOutDate = CDate(ds.Tables(0).Rows(i).Item("CheckOutDate")) 
      newInventory.mCheckInDate = CDate(ds.Tables(0).Rows(i).Item("CheckInDate")) 
      newInventory.mExpectedReturnDate = CDate(ds.Tables(0).Rows(i).Item("ExpectedReturnDate")) 
      newInventory.mUserEmailAddress = CStr(ds.Tables(0).Rows(i).Item("UserEmailAddress")) 
      newInventory.mLoanSubmitter = CStr(ds.Tables(0).Rows(i).Item("LoanSubmitter")) 
      newInventory.mDeployed = CInt(ds.Tables(0).Rows(i).Item("Deployed")) 

      result.Add(newInventory) 
     Next 
    End If 

    Return result 
End Function 

にごloaddataのを変える)新(からロードを削除しますあなたのデータ。これは最善の方法ではありませんが、正しい方向にあなたを始めるべきです。

+0

。私はmLaptopInventory =新しいInventory()とInventory.LoadData()を持っていますが、何も入力しません – Edgar

+0

@Edgarもし私が書いたものを使うなら、mLaptopInventory = Inventory.LoadData() –

0

あなたが行ゼロ選択している(付をds.Tables(0).Rows(0))と、それはのようなループにする必要があります:あなたは `ListView`を持っているん

For i As Integer = 0 To ds.Tables(0).Rows.Count-1 
    ' Get the strings here 
    ' ... 
    mFirstName = ds.Tables(0).Rows(i).Item("FirstName").ToString 
Next 
関連する問題