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

Posted on 2006-05-31
Medium Priority
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
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 16802130
.NET version?

LVL 96

Accepted Solution

Bob Learned earned 1500 total points
ID: 16802193
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

ID: 16802872
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

621 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