Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to select the top item in a listview after sorting

Tags:

c#

.net

winforms

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.

like image 525
Luke Avatar asked Nov 08 '12 22:11

Luke


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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()

like image 99
Nitay Avatar answered Nov 13 '22 01:11

Nitay


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.

like image 1
nawfal Avatar answered Nov 13 '22 00:11

nawfal