none
Listview Column Manager RRS feed

  • Question

  • I have a list view like this:

    It has lot of fields to show. What I want is to have a properties where all fields from database table will list in a checklistbox or another listview and it will check all the fields that already showing in the listview. The properties should also do arranging the fields sequence by clicking the checklistbox item with move up and move down. Just like in window file explorer properties like shown below:

    How can I do this?anyone pls...

    Saturday, May 26, 2018 8:46 AM

All replies

  • A listview is not made for a database table. A database table has items for every row. A listview only one item on a row which can be extended with subitems. 

    Use a DataGridView


    Success
    Cor

    Saturday, May 26, 2018 10:21 AM
  • Up front what is presented will work, there are plenty of things to work out.

    Here is a path to consider that does not have all the moving parts but I have tested a good deal of this based on a C# article I wrote found here. That article was based off a VB.NET code sample found here.

    First, create a class for several properties for the ListView to create columns,

    Public Interface ICloneable(Of T)
    	Function Clone() As T
    End Interface
    Public Class ListViewItemContainer
    	Implements ICloneable(Of ListViewItemContainer)
    
    	Public Property DisplayIndex() As Integer
    	Public Property Title() As String
    	Public Property Name() As String
    	Public Property Width() As Integer
    
    	Public Function Clone() As ListViewItemContainer
    		Return New ListViewItemContainer() With {.DisplayIndex = DisplayIndex, .Name = Name, .Title = Title, .Width = Width}
    	End Function
    End Class

    Use a List(Of ListViewItemContainer) to populate column information for each field to be placed into the ListView declared private to the form with the ListView. Create a second variable which will hold a clone of the original so at any point in time you can revert back to default settings.

    Private _items As New ListViewItemContainerList()
    Private _itemsClone As List(Of ListViewItemContainer)

    Here is a conceptual example which in this case is hard code.

    Public Class ListViewItemContainer
    	Implements ICloneable(Of ListViewItemContainer)
    
    	Public Property DisplayIndex() As Integer
    	Public Property Title() As String
    	Public Property Name() As String
    	Public Property Width() As Integer
    
    	Public Function Clone() As ListViewItemContainer
    		Return New ListViewItemContainer() With {.DisplayIndex = DisplayIndex, .Name = Name, .Title = Title, .Width = Width}
    	End Function
    End Class
    
    Public Class ListViewItemContainerList
    	Public Function Items() As List(Of ListViewItemContainer)
    		Return New List(Of ListViewItemContainer)() From { _
    			New ListViewItemContainer() With {.DisplayIndex = 0, .Title = "Company", .Name = "CompanyName", .Width = 200}, _
    			New ListViewItemContainer() With {.DisplayIndex = 1, .Title = "Contact", .Name = "ContactName", .Width = 130}, _
    			New ListViewItemContainer() With {.DisplayIndex = 2, .Title = "Title", .Name = "ContactTitle", .Width = 160} _
    		}
    	End Function
    End Class

    Populate the ListView and clone.

    For Each vItem In _items.Items()
    	Dim item = listView1.Columns.Add(vItem.Name, vItem.Width)
    	item.Text = vItem.Title
    Next
    
    _itemsClone = _items.Items().ConvertAll(Function(item) item.Clone())

    Populate a CheckedListBox with _items.Items

    Create a second CheckListBox (this would represent items to rearrange) where you would dump items from the first CheckedListBox into the second CheckedListBox. Note after the first time when presenting these controls the second CheckedListBox would contain current items being displayed.

    There would be a button to commit changes. In this button you would iterate the items and set DisplayIndex and Width of columns for the ListView.

    Note that visibility must be done (and I'm guessing from the width text box you realize this) by setting a column width to 0 to hide a column. 



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, May 26, 2018 11:02 AM
    Moderator
  • As Cor mentions, this will be far easier using a DataGridView.  The DataGridView doesn't care if the number of fields in a row exceed the number of columns being displayed.  For ListViewItems, this could be problematic.

    Using a DataGridView, your "Choose Details" list need only toggle the Visible property on each associated DataGridViewColumn.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 26, 2018 1:33 PM
    Moderator
  • Since several have suggested using a DataGridView here is an example which reads column data for a DataGridView from xml which is written in the modal dialog and read in through the main form with the DataGridView. Using the Up/Down buttons you can change the DisplayIndex of the DataGridView in the main form.

    Edit 5/29/2018 I decided to write a TechNet Wiki article/code sample.

    TechNet Article

    GitHub source code



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, May 27, 2018 12:35 AM
    Moderator
  • Hi zirjohn,

    Do you want to do like this?

     Dim dt As New DataTable
        Private Sub FrmListView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
            ListView3.View = View.Details
            ListView3.HeaderStyle = ColumnHeaderStyle.None
            ListView3.CheckBoxes = True
    
            ListView3.Columns.Add("", 20, HorizontalAlignment.Right)          'Add column 1
            ListView3.Columns.Add("Column 2", 250, HorizontalAlignment.Left) 'Add column 2
    
            dt.Columns.Add("details", GetType(String))
            dt.Rows.Add("AAAAAAAAA")
            dt.Rows.Add("BBBBBBBBB")
            dt.Rows.Add("CCCCCCCCCC")
            dt.Rows.Add("DDDDDDDDD")
            dt.Rows.Add("EEEEEEEEE")
            dt.Rows.Add("GGGGGGGGG")
            dt.Rows.Add("FFFFFFFFF")
            dt.Rows.Add("HHHHHHHHH")
            dt.Rows.Add("IIIIIIIII")
            dt.Rows.Add("JJJJJJJJJ")
            dt.Rows.Add("KKKKKKKKK")
            dt.Rows.Add("LLLLLLLLL")
            dt.Rows.Add("MMMMMMMMM")
            dt.Rows.Add("NNNNNNNNN")
            dt.Rows.Add("OOOOOOOOO")
            loaddata()
        End Sub
        Private Sub loaddata()
            ListView3.Items.Clear()
    
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim drow As DataRow = dt.Rows(i)
    
                If drow.RowState <> DataRowState.Deleted Then
                    Dim lvi As ListViewItem = New ListViewItem()
                    lvi.SubItems.Add(drow("details").ToString())
                    ListView3.Items.Add(lvi)
                End If
            Next
        End Sub
      Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            For Each lvi As ListViewItem In ListView3.Items
                If lvi.Checked Then
                    If lvi.Index > 0 Then
                        Dim index As Integer = lvi.Index - 1
                        ListView3.Items.RemoveAt(lvi.Index)
                        ListView3.Items.Insert(index, lvi)
                    End If
                End If
    
            Next
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            For Each lvi As ListViewItem In ListView3.Items
                If lvi.Checked Then
                    If lvi.Index < ListView3.Items.Count - 1 Then
                        Dim index As Integer = lvi.Index + 1
                        ListView3.Items.RemoveAt(lvi.Index)
                        ListView3.Items.Insert(index, lvi)
                    End If
                End If
    
            Next
        End Sub

    Here is some code about this sample, that you can take a look.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 29, 2018 5:58 AM
    Moderator
  • Hi zirjohn,

    Do you want to do like this?

    Cherry,


    Microsoft sells solutions and give tools with that as best as can be done. 

    As I told before, the Listview is not the best solution. It was used by VB6 developers because the MS FlexGrid was really terrible to use and told it was best practice not to use a Grid but instead of that a Listview.  

    I can me not imagen that at Wicresoft still this VB6 best practice is used, can you inform us if that is the case?. 


    Success
    Cor

    Tuesday, May 29, 2018 10:27 AM
  • Since several have suggested using a DataGridView here is an example which reads column data for a DataGridView from xml which is written in the modal dialog and read in through the main form with the DataGridView. Using the Up/Down buttons you can change the DisplayIndex of the DataGridView in the main form.

    Edit 5/29/2018 I decided to write a TechNet Wiki article/code sample.

    TechNet Article

    GitHub source code



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Hi Madam,

    In your example you used xml,how about when I return a datatable from mysql sql query?

    Wednesday, May 30, 2018 12:56 AM
  • Hi zirjohn,

    Do you want to do like this?

    Cherry,


    Microsoft sells solutions and give tools with that as best as can be done. 

    As I told before, the Listview is not the best solution. It was used by VB6 developers because the MS FlexGrid was really terrible to use and told it was best practice not to use a Grid but instead of that a Listview.  

    I can me not imagen that at Wicresoft still this VB6 best practice is used, can you inform us if that is the case?. 


    Success
    Cor

    I used listview because I thought it was the best control for it. I mean when I load data in datagridview, its has an effect like flickering the control, it's slow specially when scrolling down to see the more data compare to listview that seems to be more fast. But I already changed my listview to datagridview (so i have to change my code as well).
    Wednesday, May 30, 2018 12:58 AM
  • The original used XML, the updated version on GitHub uses SQL-Server table. Note I added that in to the post your just quoted. 

    The following is the script to create the data for SQL-Server.

    Class to load the data is here. The method which loads data returns the data in a DataTable. 

    Note As mentioned above, I used SQL-Server yet the final result is the same, data is placed into a DataTable.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, May 30, 2018 1:04 AM
    Moderator
  • The original used XML, the updated version on GitHub uses SQL-Server table. Note I added that in to the post your just quoted. 

    The following is the script to create the data for SQL-Server.

    Class to load the data is here. The method which loads data returns the data in a DataTable. 

    Note As mentioned above, I used SQL-Server yet the final result is the same, data is placed into a DataTable.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Hi madam,

    Here's the sample project with the class im using when im retrieving data to datagridview. Dont know where to put your sample code in my project. Please check it out..

    https://github.com/zirj0hn/vbdotnetprojects/blob/master/vbdotnetColumnProperties.rar

    I want to make ID as default column to be selected. and add a column where image will be loaded according to its gender (using imagelist key 0 as male, and 1 as female).


    • Edited by zirj0hn Wednesday, May 30, 2018 4:32 AM Added description.
    Wednesday, May 30, 2018 4:29 AM
  • In regards to not knowing where to put the code, you need to study the code and once you understand it you will know were to place the code. I took a quick look at your project and would recommend that you don't use a code module, don't work off one connection but instead use a class as I have and have a connection for each method. It's possible to do the same as in my code sample with MS-Access and have and example working with ms-access here to work with data. The only difference between this sample and the one given here is the connection.

    In regards to the new questions, they need to be placed into a new question.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 30, 2018 9:51 AM
    Moderator