FindItemWithText in WPF - c#

I want to find a text in list view then remove it I could achieve it with winforms but looks difficult with WPF here's my code :
listView1.FindItemWithText("my text", true, 0).Remove();
thanks in advance

The best way will be to write such function. Just iterate ListViewItems and delete it.
var str = "my text";
foreach (ListViewItem item in listView1.Items)
{
if (item.Content.Equals(str))
{
//... do your stuff
}
}

You could use linq
ListViewItem item = listView1.Items.Where(x => x.Content.ToString() == str).ToList()[0];

Related

Delete selected items in a GridView

IList<object> itemsSelected = MyGrid.SelectedItems;
foreach (object itemSelected in itemsSelected)
{
MyGrid.SelectedItems.Remove(itemSelected);
}
I try remove selected items from a GridView but not all selected items are removed.
Could someone help me?
object[] itemsSelected = MyGrid.SelectedItems.ToArray<object>();
foreach (object item in itemsSelected)
{
MyGrid.Items.Remove(item);
}
I had the exact problem as well. I wrote something like this but it didn't delete all items too, just some of them :
foreach(var item in MyGridView.SelectedItems)
{
MyGridView.Items.Remove(item);
}
But write this which will delete all your selected items for sure :
while (YourGridView.SelectedItems.Count != 0)
{
YourGridView.Items.Remove(YouGridView.SelectedItem);
}
there isn't exception, when you use foreach with SelectedItems? When you remove item, SelectedItems array is modified and foreach throws an exception. (Though I've tried on ListBox control). Try to use for-operator and remove items from last to first by index.
Based on your answers in the comments, I assume you are working on a C# winforms application.
Is it possible that what you are actualy using is a ListBox and not a GridView?
If that is so, you should use the ClearSelected() method which unselects all items in the ListBox.

How to select a string entry in a listbox by a given string without looping?

In WPF there's no listbox.findString.
Let's say we have a listbox:
ListBox b = new ListBox();
Then you could use LINQ:
int index = b.Items.IndexOf((
from ListBoxItem a in b.Items
where a.Content.ToString() == "something"
select a).First());
Or you can use foreach:
foreach (ListBoxItem lbi in b.Items)
{
if (lbi.Content is string && (string)lbi.Content == "something")
{
index = b.Items.IndexOf(lbi);
break;
}
}
var entries = listBox.Items.Where(item => item.ToString() == "something");
In most scenarios, you want to bind the ListBox's ItemsSource to an actual collection in your code that implements IEnumerable, then use the .Where().First() statement to find the first occurence of your string, like this:
List<string> lstb = new List<string>() { "StringA", "StringB", "StringC" };
string stringC = lstb.Where(s => s == "StringC").First();
Then if you want to programmatically select the item in your list:
yourListBox.SelectedItem = stringC;
However I strongly suggest you take the time to learn about databinding and the MVVM model which simplifies interaction with WPF controls by a lot.

How to add item from ListView to string?

Im trying to get all the items from a listview into a string like so:
foreach(ListViewItem item in ListView1.Items)
{
thisstring += item...?
}
item.Text is not a property of item...can seem to figure this out. Any suggestions?
You could use LINQ to select all items' Text.
var allItems = ListView1.Items.Cast<ListItem>().Select(i => i.Text);
var allItemText = String.Join(",", allItems);
Note that you need to add the System.LINQ namespace.
Edit: I've read ListBox, a ListView does not have a Text property and i'm not sure what text you actually want to concat.
foreach(ListViewItem item in ListView1.Items)
{
thisstring += item.Text+",";
}
thisstring.TrimEnd(',');
isn't it that simple.
StringBuilder sb = new StringBuilder();
foreach(ListViewItem item in ListView1.Items)
{
sb.Append(item.Text);
sb.Append(',');
}
Console.WriteLine(sb.ToString().TrimEnd(','));
EDIT: As Tim and Guest said, there is not Text property for ListViewItem in ASP.Net, Windows Forms has ListViewItem and it has the text property. ASP.Net ListView does not have Text property
string.Join(" ", ListView1.Items.Cast<ListItem>().Select(i => i.Text).ToArray());

Check all item in listview with huge list item?

I want to check about 3000 item in listview. This is a bit of code :
foreach (ListViewItem item in this.lvItem.Items)
{
item.Checked = !item.Checked;
}
But listview is very slow when item is checked. Please give me some ideas to solve this problem? Thanks.
I had the same problem but I found why.
I had an "ItemChecked" event handler attached to my listView that was doing some heavy stuff.
I removed the eventHandler and it solved my problem.
Try removing any "ItemChecked" eventhandler and see if the speed is better.
You need to call BeginUpdate before the loop and EndUpdate after the loop:
listView1.BeginUpdate();
foreach (ListViewItem item in listView1.Items)
item.Checked = true;
listView1.EndUpdate();
Calling BeginUpdate prevents the control from drawing until the EndUpdate method is called.
I heard a rumor that for large list items a for loop will work faster than a foreach loop
try
for(int i = 0; i = < this.1vItem.Items.Count; i++)
{
//Stuff
}
I also don't think it's wise to expect a user to click 3000 items. But something I did recently, when adding the items, knowing that there would never be many and by default they should be checked, is check the items before adding them to the list.
Something like this:
foreach (Recipient recipient in recipients)
{
var item = new ListViewItem(recipient.FirstName + " " + recipient.LastName);
item.Tag = recipient;
item.Checked = true;
lvRicipients.Items.Add(item);
}
Will something like this work for you? ...when checked, add the items to a Dictionary ...when unchecked, remove from the Dictionary. Not tested code but wondering if you could do something like this:
Dictionary<String, ListViewItem> Dic = listView.Items
.Cast<ListViewItem>()
.ToDictionary(x => x.Text, x => x.SubItems[0].Checked);
You asked how to better go about it. What I am saying is on your Check Event you will want to add items to your list view. I doubt that a user will actually check all 3000, so change your code to decide how you would want to handle checked items, the example that I have given you uses Lambda expression. If not familiar, then please alter your question to reflect what it is that you actually need and/or want...
for (int i = 0; i <= listView1.Items.Count - 1; i++)
{
if (!listView1.Items[i].Checked)
listView1.Items[i].Checked = true;
}

How to clear all Groups And Items in Listview Control

How to clear all Groups And all Items in that Groups in Listview Control
Probably ListView.Clear() will work for you. And to clear groups in ListView call ListViewGroupCollection.Clear()
if you are filling your group items with datasource then you could try something like this..
How about
DataSource = null;
DataBind();
If you want to remove only the listViewItems which are grouped, you can do following:
foreach (var group in listView.Groups)
{
var listViewItemsToDelete = listView.Items.Cast<ListViewItem>().Where(item => Equals(item.Group, group));
foreach (var itemToRemove in listViewItemsToDelete)
{
listView.Items.Remove(itemToRemove);
}
}
listView.Groups.Clear();

Categories

Resources