Two conditions checking on listview group items
I have list view (with one column). I have divided this list view into two groups like in the figure below.
My problem is: how can I find selection event like this: If I click on odd group item ("one") and then if I click on even group item ("two"), I want to do something.
How can I check these two conditions in a single if
statement? These two conditions are the ones that need to be done in a single if statement. Is it possible to use a single condition?
I am using C# and WinForms apps. Would anyone please help on this?
EDIT :
Category names
name 1
name 2
name 3
prices
>100
开发者_运维技巧 >200
300+
If I click on category name and then I click on the price range, I want to do something. Is it possible to do both in single condition checking?
Try using the SelectedIndexChanged event like so:
void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedItems = listView1.SelectedItems.Cast<ListViewItem>();
var passed = (selectedItems.Select(l => l.Group.Name).Distinct().Count() == 2 && selectedItems.Count() == 2);
if (passed)
{
//Do something...
}
}
Edit (based on comments)
To retrieve each selected item use the following:
Note: For the below code to work you would need to set the Name property of each of your ListViewGroup's to "Category" and "Prices" respectively.
void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedItems = listView1.SelectedItems.Cast<ListViewItem>();
var passed = (selectedItems.Select(l => l.Group.Name).Distinct().Count() == 2 && selectedItems.Count() == 2);
if (passed)
{
var categoryItem = selectedItems.Where(l => l.Group.Name.ToLower() == "category").Single();
var priceItem = selectedItems.Where(l => l.Group.Name.ToLower() == "prices").Single();
}
}
精彩评论