5

I'm filling my System.Windows.Forms.ListView with results from my database as such:

foreach (DataRow row in theTable.Rows)
{
    ...build item from row..

    myListView.Items.Add(item);
}

And then I want to sort my listview in a different order than the rows come back from the DB, so I call

myListView.Sort();

But then when I want to go to select the top item in the listview it won't work, it selected something other than the top item:

myListView.Items[0].Selected = true;

Makes sense since the Items collection is added to in the order of the rows from the table iterated through in the foreach loop.

Using myListView.TopItem.Seleted = true doesn't work either.

So how do I go about selecting the topmost item in the listview AFTER I've sorted it?

Thanks for any answers.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
Luke
  • 1,218
  • 1
  • 24
  • 40

2 Answers2

5

Are you sure the list view is currently selected? If it's not selected you will not see the item being selected.

The following code seem to be working:

    private void Populate(object sender, EventArgs e)
    {   
        listView1.Items.Add("D");
        listView1.Items.Add("B");
        listView1.Items.Add("A");
        listView1.Items.Add("C");
    }

    private void SelectFirst(object sender, EventArgs e)
    {
        listView1.Items[0].Selected = true;
        listView1.Select();
    }

    private void SortAndSelect(object sender, EventArgs e)
    {
        listView1.Sorting = SortOrder.Ascending;
        listView1.Sort();

        listView1.Items[0].Selected = true;
        listView1.Select();
    }

Notice the listView1.Select()

Nitay
  • 4,193
  • 6
  • 33
  • 42
  • I'm trying the above code on a blank form right now and D is being selected, which is the problem that I'm describing in my original post. I'm wondering about a way for it to come away with A selected here... – Luke Nov 08 '12 at 23:39
  • After you call SortAndSelect - D is still selected? What .NET Version are you using? – Nitay Nov 09 '12 at 00:09
  • 1
    Yeah, see screenshots below. (Iflipped the order of the list a bit from yours so it selects C) Using VS2005 .NET2 http://i44.photobucket.com/albums/f38/ZGangsta/Untitled3.png http://i44.photobucket.com/albums/f38/ZGangsta/Untitled.png – Luke Nov 09 '12 at 00:15
  • Hmm, it works now in Form_Load. In my actual code I'm doing this called from an Initialize function I've set up which is being called between the Form's initialization and its ShowDialog call... Any idea why the difference? – Luke Nov 09 '12 at 00:22
  • 1
    The form layout must occur before you can play with all the controls. I'm not sure on the details, but I think it occurs only after the constructor. See http://stackoverflow.com/questions/2623808/form-constructor-vs-form-load – Nitay Nov 09 '12 at 00:31
1

You probably have HideSelection set to true. Make it false, and try.

myListView.HideSelection = false;

Furthermore listviews can have a selected item but with no focus on some other item. So its better to set both focus and selection together:

if (myListView.Items.Count > 0)
{
    myListView.Items[0].Selected = true;
    myListView.Items[0].Focused = true;
}

If that doesn't work you can set focus to listview itself to see if selection is falling on right item.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • No, HideSelection is false in my code. I can see the item being selected. As I said, it's that it's selecting what was the top item when they were added to the list, not the top item after the sort. – Luke Nov 08 '12 at 23:30
  • @Luke are u sure you are calling the set focusing part after sorting? – nawfal Nov 08 '12 at 23:33
  • Yes, after sorting. I fill the listview, sort, then set the selected/focused item to Items[0] and the top item in the list doesn't select, what selects is the first item that was added to the collection (which makes sense). I'm not having a problem seeing the selection, what I'm wondering is how to select the item that is the top after the sort. – Luke Nov 08 '12 at 23:41
  • @Luke I tried your code and it worked for me very well. Something is strange in your case. It would be better to see a screenshot of your listview before and after sorting and setting of focus – nawfal Nov 08 '12 at 23:45
  • Thanks for taking a look. This is a simple example of the code (from Nitay's example below) http://i44.photobucket.com/albums/f38/ZGangsta/Untitled2.png And this is a screenshot as soon as the form opens http://i44.photobucket.com/albums/f38/ZGangsta/Untitled.png So you can see how C is selecting when I want it to be A. – Luke Nov 09 '12 at 00:03
  • @Luke thats so strange. Doesnt happen with me. Your code in link gives me the correct selection of top item. – nawfal Nov 09 '12 at 00:11
  • see bewlow in comments on Nitay's answer. It works for me if I move the code to Form_Load. – Luke Nov 09 '12 at 00:22