Results 1 to 2 of 2

Thread: [2005] Can same code be implement for all forms in listview?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    [2005] Can same code be implement for all forms in listview?

    I had a few forms which has listview controls. In the listview, the contents shown inside all came from an access file.
    I wanted to ask about the coding part. Can I use the same coding below for all my list forms, of course some parts need to be changed)? But will that results in any corruption?

    I tried to implement the same code on my another form but it fails to show the contents...

    And so will this code I made be sufficient? Thanks in advance for any help given...

    Code:
       Private Sub LoadProjects()
            'Declare variables
            Dim objListViewItem As ListViewItem
    
            'Initialize a new instance of the data access base class
            Using objData As New WDABase
                Try
                    'Get all projects in a DataReader object
                    objData.SQL = "usp_SelectProjects"
                    objData.InitializeCommand()
                    objData.OpenConnection()
                    objData.DataReader = objData.Command.ExecuteReader
    
                    'See if any data exists before continuing
                    If objData.DataReader.HasRows Then
    
                        'Clear previous list
                        lvwProjects.Items.Clear()
    
                        'Process all rows
                        While objData.DataReader.Read()
    
                            'Create a new ListViewItem
                            objListViewItem = New ListViewItem
    
                            'Add the data to the ListViewItem
                            objListViewItem.Text = _
                                objData.DataReader.Item("PartName")
                            objListViewItem.Tag = objData.DataReader.Item("PartID")
    
                            'Add the sub items to the listview item
                            objListViewItem.SubItems.Add( _
                                objData.DataReader.Item("PartDescription"))
                            objListViewItem.SubItems.Add( _
                                objData.DataReader.Item("SequenceNumber"))
                            objListViewItem.SubItems.Add( _
                                Format(objData.DataReader.Item("LastUpdateDate"), "g"))
    
                            'Add the ListViewItem to the ListView control
                            lvwProjects.Items.Add(objListViewItem)
    
                        End While
    
                    End If
    
                    objData.DataReader.Close()
                Catch ExceptionErr As Exception
                    MessageBox.Show(ExceptionErr.Message, strAppTitle)
                End Try
            End Using
    
            'Cleanup
            objListViewItem = Nothing
    
        End Sub
    
        Private Sub lvwProjects_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles lvwProjects.Click
    
            'Initialize a new instance of the data access base class
            Using objData As New WDABase
                Try
                    'Get the specific project selected in the ListView control
                    objData.SQL = "usp_SelectProject"
                    objData.InitializeCommand()
                    objData.AddParameter("@PartID", Data.OleDb.OleDbType.Guid, 16, _
                        lvwProjects.SelectedItems.Item(0).Tag)
                    objData.OpenConnection()
                    objData.DataReader = objData.Command.ExecuteReader
    
                    'See if any data exists before continuing
                    If objData.DataReader.HasRows Then
    
                        'Read the first and only row of data
                        objData.DataReader.Read()
    
                        'Populate the Project Details section
                        txtProjectID.Text = _
                            objData.DataReader.Item("PartID").ToString.ToUpper
                        txtProjectName.Text = _
                            objData.DataReader.Item("PartName")
                        txtProjectDescription.Text = _
                            objData.DataReader.Item("PartDescription")
                        txtSequenceNumber.Text = _
                            objData.DataReader.Item("SequenceNumber")
                        txtProjectUpdateDate.Text = _
                            Format(objData.DataReader.Item("LastUpdateDate"), "g")
    
                    End If
    
                    objData.DataReader.Close()
                Catch ExceptionErr As Exception
                    MessageBox.Show(ExceptionErr.Message, strAppTitle)
                End Try
            End Using
        End Sub
    This is the code I used for my 1st listview control but it cannot seem to be working for my other controls...

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Can same code be implement for all forms in listview?

    You have a couple of options here, namely either making a custom control which inherits from ListView, then writing in your own methods to extend this (such as a populate method), or declaring a base class which your forms can then use.

    The 2nd will be the easiest and you can use similar to the following code in order to implement this:
    Code:
    Imports System.Data
    
    Public MustInherit Class myFormBaseClass
        Inherits System.Windows.Forms.Form
    
        Protected Sub PopulateListview(ByVal listviewControlToPopulate As ListView)
            ' Code which calls GetListviewDataReader then performs your loop
            ' in order to add (any returned) items/records to the passed-in
            ' listviewControlToPopulate (form listview).
        End Sub
    
    
        Private Function GetListviewDataReader() As IDataReader
            ' Code to instanciate your WDABase class and retreive a datareader 
            ' object - are you disposing this & it's child objects  correctly?
        End Function
    
    End Class
    Code:
    Public Class Form1
        Inherits windowsapplication4.myFormBaseClass
    
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.load
            MyBase.PopulateListview(Me.ListView1)
        End Sub
    
    End Class

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width