Hi,
I need code to invert the selected items in list view.
Can any one help me out..
Thanks in advance for help..
Regards,
Sravanthi Ch
Hi,
I need code to invert the selected items in list view.
Can any one help me out..
Thanks in advance for help..
Regards,
Sravanthi Ch
Quick summary and clarification: the goal is to flip selection so every currently selected item becomes unselected and every currently unselected item becomes selected. pointed to the simple per-item toggle and showed a safe index-based approach; both work, but which one to use depends on whether you are using WinForms or WPF, whether the control is virtualized, and whether you need to suppress selection-change events while updating.
WinForms notes (System.Windows.Forms.ListView)
MultiSelect is true. BeginUpdate() / EndUpdate(). SelectedIndexChanged handler, temporarily unsubscribe it while changing selection to avoid reentrancy.SelectedItems while iterating it. Use the SelectedIndices collection if you want to work by index.WPF notes (System.Windows.Controls.ListView)
Items[i].Selected and that member is missing, you are likely in WPF: selection is on the container (ListViewItem.IsSelected) or via the view-model. Containers may be null when virtualization is on, so the robust pattern is to bind selection to your data model and toggle that property. Example patterns:for (int i = 0; i < myWpfListView.Items.Count; i++)
{
var container = myWpfListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
if (container != null) container.IsSelected = !container.IsSelected;
else /* toggle your item model's IsSelected property if you expose one */;
} XAML pattern (bind container IsSelected to item property):
<ListView ItemsSource="{Binding Items}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
</ListView> Troubleshooting quick tips
HideSelection or focus state. EnsureVisible (WinForms) or ScrollIntoView (WPF) if you need to reveal newly selected items. These points tie back to the suggestions from and and answer ’s question about “how” to invert by showing which API and pattern is right depending on the framework and virtualization.
Jump to Post— Mitja Bonca 557How to invert?
From last to 1st? By which column (items, or subItems)?I need more information.
Jump to Post— Egop29 0Hi, try this
private void button1_Click(object sender, EventArgs e) { for (int k = 0; k < listV.Items.Count; ++k) { listV.Items[k].Selected = !(listV.Items[k].Selected); } listV.Select(); }
How to invert?
From last to 1st? By which column (items, or subItems)?
I need more information.
Hi, try this
private void button1_Click(object sender, EventArgs e)
{
for (int k = 0; k < listV.Items.Count; ++k)
{
listV.Items[k].Selected = !(listV.Items[k].Selected);
}
listV.Select();
} Hi, try this
private void button1_Click(object sender, EventArgs e) { for (int k = 0; k < listV.Items.Count; ++k) { listV.Items[k].Selected = !(listV.Items[k].Selected); } listV.Select(); }
Hi,
I am not gettign the selected property in listV.Items[k].Selected..
Any how i got the solution for it..
Thanks for replying.
Regards,
Sravanhi Ch
Would you mind telling me exactly what would you like to invert? I have no exact clue now, what you would you like to have. Thx in advance.
Hi,
Here is the code i have used in my application.
Here i need to unselect the selected items and select the unselected item in a button click which means i need to invert the selected items in a list view.
// Storing the selected items in an int List
List<int> _intList = new List<int>();
for (int k = 0; k < MyListView.SelectedItems.Count; k++)
_intList.Add(MyListView.Items.IndexOf(MyListView.SelectedItems[k]));
// Clearing the selected items
MyListView.SelectedItems.Clear();
// Inverting
for (int i = 0; i < MyListView.Items.Count; i++)
{
if (!_intList.Contains(i))
MyListView.SelectedItems.Add(MyListView.Items[i]);
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.