如何使用类的对象填充datagridview

时间:2018-06-09 23:27:17

标签: vb.net

好的,所以我觉得自己很困惑。我正在研究班级库存数据库。我的计划是让一个人在文本框中输入他们的信息和一本书的信息,我希望将这些信息放入lstbox(基本上说谁现在租用这本书)。我还有一个Datagridview,我希望我的txtfile打开并显示书籍,但只有我的txtfile中的对象(Title,Author,ISBN,YearPublishished)。我尝试用查询来做,但它似乎没有工作。

我有一些我需要帮助的错误

  1. 当我启动应用程序并单击显示书籍时。我的OFD打开,我单击我需要的txt文件,但它没有正确显示到我的DataGridView。 IT仅显示列中的数字。

  2. 我们不得不使用类的对象,我只想知道我是否正确使用它们。这是我第一次这样做,所以任何帮助都会受到赞赏。

  3. 我附上了我的申请表

  4. 好的,所以我到目前为止:

    Public Class Library
    
    
    Implements IComparable
    
    
        'Private Instance Variables - Always declared as Private
        Private m_Title As String
        Private m_Author As String
        Private m_ISBN As String
        Private m_YearPublished As Date
        Private m_DateRented As Date    'use maskedtxtbox for dates and phone number
        Private m_DueDate As Date
        Private m_Name As String
        Private m_Email As String
        Private m_PhoneNumber As String
    
        Private Shared LibraryCount As Integer
    
        'Property Blocks for each Private Instance Variable
        Public Property Title() As String
            Get
                Return m_Title
            End Get
            Set(value As String)
                m_Title = value
            End Set
        End Property
    
        Public Property Author() As String
            Get
                Return m_Author
            End Get
            Set(value As String)
                m_Author = value
            End Set
        End Property
    
        Public Property ISBN As String
            Get
                Return m_ISBN
            End Get
            Set(value As String)
                m_ISBN = value
            End Set
        End Property
        Public Property YearPublished As Date
            Get
                Return m_YearPublished
            End Get
            Set(value As Date)
                m_YearPublished = value
            End Set
        End Property
        Public Property DateRented As Date
            Get
                Return m_DateRented
            End Get
            Set(value As Date)
                m_DateRented = value
            End Set
        End Property
    
        Public Property DueDate As Date
            Get
                Return m_DueDate
            End Get
            Set(value As Date)
                m_DueDate = value
            End Set
        End Property
    
    
    
        Public Property Name As String
            Get
                Return m_Name
            End Get
            Set(value As String)
                m_Name = value
            End Set
        End Property
    
        Public Property Email As String
            Get
                Return m_Email
            End Get
            Set(value As String)
                m_Email = value
            End Set
        End Property
    
        Public Property PhoneNumber As String
            Get
                Return m_PhoneNumber
            End Get
            Set(value As String)
                m_PhoneNumber = value
            End Set
        End Property
    
    
        Public Sub New()
            m_Title = ("")
            m_Author = ""
            m_ISBN = ""
            m_DateRented = ""
            m_DueDate = ""
            m_Name = ""
            m_Email = ""
            m_PhoneNumber = 0
            LibraryCount += 1
        End Sub
    
        'Overloaded /Parameterized Constructors
    
        Public Sub New(ByVal p_Title As String, ByVal p_Author As String, ByVal p_ISBN As String, ByVal p_YearPublished As Date,
                   ByVal p_DateRented As Date, ByVal p_DueDate As Date, ByVal p_Name As String, ByVal p_Email As String, ByVal p_PhoneNumber As String)
            m_Title = p_Title
            m_Author = p_Author
            m_ISBN = p_ISBN
            m_YearPublished = p_YearPublished
            m_DateRented = p_DateRented
            m_DueDate = p_DueDate
            m_Name = p_Name
            m_Email = p_Email
            m_PhoneNumber = p_PhoneNumber
            LibraryCount += 1
        End Sub
    
    
        'QUESTION? How would I calculate when the books is due
        '  Public Function CalcDueDate() As String     'person gets charged if they are late returning the book    
    
        ' If m_DateRented.Date > m_DueDate.Date Then
        'm_DueDate = m_DateRented + 10
        'MessageBox.Show("You will be charged with a late of .50 cents")
        'End If
        'End Function
    
        Public Overrides Function ToString() As String
            Return "The user rented a book on" & DateRented & " it will be due on" & DueDate
        End Function
    
        Public Function CalcDueDate(ByVal p_DueDate As Date) As Date
    
            Return p_DueDate
        End Function
        Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
    
            Throw New NotImplementedException()
            Return m_YearPublished.CompareTo(CType(obj, Library).m_YearPublished)
    
        End Function
    
        Public Overrides Function Equals(obj As Object) As Boolean
            'create a library object from the paramer - check to see if it exist
            Dim libraryobj As Library = TryCast(obj, Library)
            If libraryobj Is Nothing Then
                Return False
            Else
                'code for the comparison based on m_yearpublished
                Return m_YearPublished.Equals(libraryobj.m_YearPublished)
            End If
        End Function
    
    
    End Class
    
     End Class
    
    
    
    
    
    Public Class Project1Data
    
    'create counter variable for the array
    Dim counter As Integer = 0
    'create an array of objects
    Private books(12) As Library
    'Collection List
    Dim mybooklist As New List(Of Library)
    
    
    Dim abook As Library
    
    
    
    Private Sub btnDisplayBooks_Click(sender As Object, e As EventArgs) Handles btnDisplayBooks.Click
    
        Dim textfile As String
        OpenFileDialog1.ShowDialog()
        textfile = OpenFileDialog1.FileName
        DGVbooks.DataSource = IO.File.ReadAllLines("LibraryDatabase")
    
    End Sub
    
    Public Sub Data()
    
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'The Btn_Add method creates a Renter and book and adds it to the collectio
    
        'validate input data
        If ValidateData() = -1 Then
            Exit Sub
        End If
    
        'use valid input to create an object of the class by calling overloaded constructor
        Dim bookk As New Library(txtbooktitle.Text, txtbookauthor.Text, txtISBN.Text, CDate(txtyearpublished.Text),
                                CDate(txtdaterented.Text), CDate(txtduedate.Text), txtname.Text, txtemail.Text, (txtphonenunber.Text))
    
    
        'error check that the array is not full
        If counter >= books.Length Then
            ReDim Preserve books(counter + 1)
        End If
    
        'add book to the array
        books(counter) = bookk
        'add book to the list
        mybooklist.Add(bookk)
    
        'increment counter
        counter = counter + 1
    
        'Notify user that the rental has been recorder
        MessageBox.Show("Rental recorded")
    
        lstbooksoutput.DataSource = Nothing
        lstbooksoutput.DataSource = books
    
    End Sub
    
    Private Sub Project1Data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim lines() As String = IO.File.ReadAllLines("LibraryDatabase")
    
    
        'In the DGV I just want to display the title, author, isbn, and year published
        ' In the lstbox the user name, email, and phone number will be displayed
        'When a user enters a new book it adds itself to the DGV
    
    
        Dim query = From abook In books
                    Select abook.Title, abook.Author, abook.ISBN, abook.YearPublished
                    Order By abook.YearPublished Ascending
        DGVbooks.DataSource = query.ToList()
    

    好的,我在这里使用重载构造函数创建了一个类的对象,我想我已将它发送到datagridview但它没有显示。

        'Populate a list with Library objects
    
        With mybooklist
            .Add(New Library("Innoncent Injustice(A Chance Reddick Thriller Book 1), David Archer,B07D6442LM", 2018))
            .Add(New Library("A Breath Of Witchy Air(A Wicked Witches Of the Midwest Mystery), Amanda M Lee, B07CRLRGXH", 2018))
            .Add(New Library("Booke Of the Hidden, Jeri Westerson, 978 - 1635760507", 2017))
            .Add(New Library("Wolves Of Wisteria(Wisteria Witches Mystery), Angela Pepper,978-1719549591", 2018))
            .Add(New Library("Soul Render(Soul Stones Book 1), TL Branson,978-1980871392", 2018))
            .Add(New Library("A Thrift Shop Murder(Cats And Ghosts And Avocado Toasts Book 1), Stanford Pritchard,978-1773480114", 2018))
            .Add(New Library("Cathadeus, Jeff J Peters,B077ZNHY7T", 2017))
            .Add(New Library("Clockwork Alchemist (Thief's Apprenctice Series Book 1), Sara C. Roethle,B01MG26KNA", 2016))
            .Add(New Library("Air Awakens Series, Elise Kova,B01N4A2TK5", 2016))
            .Add(New Library("Keeper of the Dragons The Prince Returns (Keeper of Dragons Book 1), J.A Cullican,B01FYL5BD0", 2016))
            .Add(New Library("Nightblade A Book of UnderRealm, Garrett Robinson,978-1941076309", 2014))
            .Add(New Library("Crazy Rich Asians, Kevin Kwan,B00AP2VQEM", 2013))
            .Add(New Library("Poison Princess (The Arcana Chronicles Book 1), Kresley Cole,978-1442436640", 2012))
    
        End With
    
        DGVbooks.DataSource = query.ToList
            DGVbooks.Columns("Title").HeaderText = "Title"
            DGVbooks.Columns("Author").HeaderText = "Author"
            DGVbooks.Columns("ISBN").HeaderText = "ISBN"
            DGVbooks.Columns("Year Publsihed").HeaderText = "Year Published"
    
            DGVbooks.DataSource = mybooklist
    End Sub
    
    Function ValidateData() As Integer
        'This validates the data in all of the textboxes
        'Invalid data returns a -1, else 1
        If txtbooktitle.Text = "" Then
            MessageBox.Show("Please enter a Book Title")
            Return -1
        ElseIf txtbookauthor.Text = "" Then
            MessageBox.Show("Please enter a Book Title")
            Return -1
        ElseIf txtdaterented.Text = "" Or IsNumeric(txtdaterented.Text) = False Then
            MessageBox.Show("Please enter a valid date")
            Return -1
        ElseIf txtduedate.Text = "" Or IsNumeric(txtduedate.Text) = False Then
            MessageBox.Show("Please enter a valid date")
            Return -1
        ElseIf txtyearpublished.Text = "" Or IsNumeric(txtyearpublished.Text) = False Then
            MessageBox.Show("Please enter a valid date")
            Return -1
        ElseIf txtname.Text = "" Then
            MessageBox.Show("Please enter a name")
            Return -1
        ElseIf txtemail.Text = "" Then
            MessageBox.Show("Please enter a valid email")
            Return -1
        ElseIf txtphonenunber.Text = "" Or IsNumeric(txtphonenunber.Text) = False Then
            MessageBox.Show("Please enter a valid phone number")
            Return -1
        Else
            Return 1
        End If
    
    End Function
    
    
    
    
    Private Sub lstbooksoutput_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbooksoutput.SelectedIndexChanged
        If lstbooksoutput.SelectedIndex > -1 Then
            Dim intbook As Integer = lstbooksoutput.SelectedIndex
            'get information for that user in the array
            MessageBox.Show(books(intbook).Name & "  has rented" & books(intbook).Title & ".  The book will be due on " & books(intbook).DueDate)
    
        End If
    End Sub
    
    
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'clear the textboxes
        txtbooktitle.Clear()
        txtbookauthor.Clear()
        txtISBN.Clear()
        txtdaterented.Clear()
        txtduedate.Clear()
        txtyearpublished.Clear()
        txtname.Clear()
        txtemail.Clear()
        txtphonenunber.Clear()
    
        'reset focus to title box
        txtbooktitle.Focus()
    End Sub
    

    结束班

    enter image description here

1 个答案:

答案 0 :(得分:0)

好的悉尼,让我们开始工作吧!

  1. 要么你严重忽略了你的学业,要么你的导师 让你挣扎;在这种情况下,你应该通知你的 大学,你没有得到你付出的代价。
  2. 您的对象包含太多数据。您需要一个书籍对象,一个客户对象和一个租赁对象。假设图书馆馆长 想要所有客户的清单。在你的模型中你会得到每个 客户列出了很多次,获得导演将是一个烂摊子 他的名单。
  3. 如果可以,请使用自动属性。我认为它们在VS 2015中可用。您只需要将公共属性标题写为String 而精彩的vb编译器会编写get,set和 Itermedite语言代码中的私有字段。 (你赢了 在源代码中看到它,但它就在那里)
  4. 现在是你的文字档案。每本书一行。本书的每个属性都应该用一个没有出现在其他地方的字符分隔 文件。管道符号(|)(大写反斜杠)怎么样 所以你文件中的一行看起来像...... 1 | Innoncent Injustice(A. 机会雷迪克惊悚书1)|大卫阿彻| B07D6442LM | 2018通知 我添加了另一个字段BookID
  5. 我还没有包含默认构造函数(没有参数的构造函数)。这会强制创建书籍对象的任何代码提供 所有参数的值,即使它只是""。
  6. Public Class Book
        Public Property BookID As Integer
        Public Property Title As String
        Public Property Author As String
        Public Property ISBN As String
        Public Property YearPublished As Integer
        Public Sub New(id As Integer, bTitle As String, bAuthor As String, bISBN As String, bPublished As Integer)
            BookID = id
            Title = bTitle
            Author = bAuthor
            ISBN = bISBN
            YearPublished = bPublished
        End Sub
    End Class
    
    Public Class Project1Data
        Private lstBooks As New List(Of Book)
        Private Sub Project1Data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim textfile As String
            OpenFileDialog1.ShowDialog()
            textfile = OpenFileDialog1.FileName
            Dim lines() As String = IO.File.ReadAllLines(textfile)
    
            For Each line As String In lines
                Dim fields() As String = line.Split("|"c) 'the little c after "|" indicates that this is a char
                'String.Split takes a char
                Dim b As New Book(CInt(fields(0)), fields(1), fields(2), fields(3), CInt(fields(4)))
                lstBooks.Add(b)
            Next
            DataGridView1.DataSource = lstBooks
        End Sub
    End Class