how to get the selected item of a listview by column name?

Posted on 2006-05-31
Last Modified: 2007-12-19
i want to get the user selected columns of a unbound listview by thir column name. I have build the view like this:

        With lvMaster
            .Columns.Add("ID", 0, HorizontalAlignment.Right)
            .Columns.Add("Group", 100, HorizontalAlignment.Left)
            .Columns.Add("Comment", 200, HorizontalAlignment)
        End With

        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Do While (dr.Read())
            lvItem = New ListViewItem(dr.Item("ID").ToString)
            Call lvMaster.Items.Add(lvItem)

    Private Sub lvMaster_selectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                     Handles lvMaster.SelectedIndexChanged

        'this doesn't work
        Dim col As ListView.ColumnHeaderCollection = lvMaster.Columns
        Dim boo As Boolean = col.ContainsKey("Group")   'always = false
        Dim idx As Integer = col.IndexOfKey("Group")      'always = -1

        'this works, but col is selected by index rather than by name
        Dim s as string = lvMaster.SelectedItems(0).SubItems(1).Text
   End Sub

Has anyone an idea how ro select  the column by their header name?

Question by:CumulusGranitus
    LVL 96

    Expert Comment

    by:Bob Learned
    .NET version?

    LVL 96

    Accepted Solution

    Aaah, yes, 2005 ;)

    1) ContainsKey looks for columns by name and not by text.  

    2) "Group" is the heading text for the column, and not the name, since you used the (text, width, textAlign) form of the ColumnHeader constructor.

    3) If you want to use key, you need to use the (key, text, width, textAlign) form of the ColumnHeader constructor.

     lv_Master.Columns.Add("ID", "ID", 0, HorizontalAlignment.Right)
     lv_Master.Columns.Add("Group", "Group", 100, HorizontalAlignment.Left)
     lv_Master.Columns.Add("Comment", "Comment", 200, HorizontalAlignment)


    Author Comment

    Sorry i forgot. Yes it is VB.NET 2005 under Framework 2.0 .

    The Add method dows not allow to give the alignment parameter if you specify the first 2 parameters. So the solution that works looks like

       v_Master.Columns.Add("Group", "Group", 100 )

    That works very well. Thank you.
    Now i have to look how to specify the alignment for the column....



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Hide url in popup window 2 30
    Best platform to port vba application to? 7 35
    Better way of writing the code 4 23
    GridDataView 6 17
    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now