Click here to Skip to main content
15,903,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying out List View controls these past few days and I am having trouble controlling the click-able field in the details view of the list view. The issue is that I have list of people: first name, last name and a unique ID number. When I add the records to the list view I understand that the list view item is the click-able field not the sub-items but how can you control which column the item is added under.

To explain a little better the columns are in the order of ID, Last Name, First Name. I'm trying to add the item to the last name column and make this the click-able field.

I understand this might have something to do with the display index of the columns but there is also some kind of index beside the columns themselves as well when you add them at design time. If anyone could explain the difference between these as well this might help me understand a little more.

Any help is most appreciated. :)
Posted

I believe you may need to calculate which column you are over in your click based on the width of the columns. I don't think the listview is a grid, there are grid controls for that. I think it just knows what row you clicked.
 
Share this answer
 
It sounds like you're asking two questions. The first is how to add SubItems to a ListView. Here's some sample code to set up a new ListViewItem with the following fields: Date, Time, LastName, FirstName. Notice that I didn't have the time, so I left it blank...but you still have to add something.

C#
ListViewItem item1 = new ListViewItem("3/25/2010");
item1.SubItems.Add("");
item1.SubItems.Add("Jeffers");
item1.SubItems.Add("William");
listView1.Items.Add(item1);


As far as sorting the items based on a specific column...have a look here: How to sort a ListView control by a column in Visual C#[^]

It's also important to note that while the ListViewItem has SubItems, the first SubItem is actually the first column's value. So, if you want LastName by the order you set, you would need SubItems[1].
 
Share this answer
 
Comments
Walby 29-May-12 11:37am    
Sorry for the extremely late accept, I was reviewing all my questions and noticed I hadn't accepted a solution for this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900