Asked by:
how to see if Listview column contains a specific name

Question
-
User-1016473172 posted
I have created a ListView with multiple columns
(This is a part of the Xaml)
<ListView.View> <GridView> <GridViewColumn Width="100" Header="Indexer" DisplayMemberBinding="{Binding Number}"/> <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding NameId}"/> <GridViewColumn Width="100" Header="Age" DisplayMemberBinding="{Binding Age}"/> </GridView> </ListView.View>
cs part
public class MyItem { public int Number { get; set; } public int Age { get; set; } public string NameId { get; set; } }
i know how to add a new item to a specific column
AccountList.Items.Add(new MyItem { Age = Convert.ToInt32(AgeVAlue), NameId = NameValue.ToString(), Number = ListCount });
how do i check if NameId contains a spefic word because i cannot do this:
AccountList.Items.Contains(SpecificWord)
Saturday, December 12, 2020 10:41 PM
All replies
-
User-1716253493 posted
if(!AccountList.Items.Contains(NameValue.ToString())) { AccountList.Items.Add(new MyItem { Age = Convert.ToInt32(AgeVAlue), NameId = NameValue.ToString(), Number = ListCount }); }
Saturday, December 12, 2020 11:44 PM -
User-1016473172 posted
it still does not seem to work
i have created a checkbox named "Namecheckbox"
if its not checked it should not allow duplicate names.
string NameValue = "jacke"; if (NameCheckbox.IsChecked == false && AccountList.Items.Contains(NameValue.ToString()) ) { MessageBox.Show("this name is already in use"); } else if (NameCheckbox.IsChecked == true) { MessageBox.Show("ok"); } else { MessageBox.Show("something went wrong"); }
when i have added the name "jacke" to the list and i use the above code it does not warn me with the text "this name is already in use"
or when i just use this:
if (AccountList.Items.Contains(NameValue.ToString())) { MessageBox.Show("already used"); }
after i put the word jacke in the list and i use this code (as a button ) nothing is showing
Sunday, December 13, 2020 2:16 AM -
User-939850651 posted
Hi arclite860,
According to your description, have you tried something like this?
public MainWindow() { InitializeComponent(); List<MyItem> items = new List<MyItem>(); items.Add(new MyItem() { NameId = "John Doe", Age = 42, Number = 1 }); items.Add(new MyItem() { NameId = "Jane Doe", Age = 39, Number = 2 }); items.Add(new MyItem() { NameId = "Sammy Doe", Age = 13, Number = 3 }); listView.ItemsSource = items; string NameValue = "Jane Doe"; bool used = false; foreach (var item in listView.Items) { if (((MyItem)item).NameId == NameValue) used = true; } MessageBox.Show(used?"Name existed":"not exist"); } public class MyItem { public int Number { get; set; } public int Age { get; set; } public string NameId { get; set; } } private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e) { }
Result:
Best regards,
Xudong Peng
Wednesday, December 16, 2020 9:27 AM