for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
}
}
This function simply deletes the selected item in listview.. but i want to cut it and paste it somewhere else.
It sounds like you want to remove the selected listitems and move them to another listview.
ListView sourceListView = new ListView();
ListView destListView = new ListView();
var selected = sourceListView.Items
.Cast<ListViewItem>()
.Where(x => x.Selected)
.ToList();
foreach (var item in selected)
{
sourceListView.Items.Remove(item);
destListView.Items.Add(item);
}
Related
I have a text file like this:
==
a
03/09
==
b
02/09
And I want to create a listView from the text file like this:
How can I do this in C#?
I've tried this code:
ListViewItem lvi = new ListViewItem();
var sectionCharacters = File.ReadLines("bdaylist.list")
.SkipWhile(s => s != "==")
.Skip(1)
.Skip(2)
.ToList();
lvi.Text = sectionCharacters[1];
lvi.SubItems.Add(sectionCharacters[2]);
listView1.Items.Add(lvi);
but it only read b and 02/09
You can try this:
var sectionCharacters = File.ReadLines("your_filepath_here").ToList();
//To remove '=='
sectionCharacters.Where(i => i.Trim() == "==").ToList()
.ForEach(item => sectionCharacters.Remove(item));
//To remove 'blank lines', if any
sectionCharacters.Where(i => i.Trim() == "").ToList()
.ForEach(item => sectionCharacters.Remove(item));
for (int i = 0; i < sectionCharacters.Count; i += 2)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = sectionCharacters[i];
lvi.SubItems.Add(sectionCharacters[i + 1]);
listView1.Items.Add(lvi);
}
Output:
PS: This is a simple listView to show the output. You might get the idea what to do with your listView having checkboxes.
You are using Skip(1) and Skip(2) which is ignoring your first 2 entries. You are also only adding one item to the list. You can change your linq as below, and check for the even number item in the collection to add both to the listview.
var sectionCharacters = File.ReadAllLines("bdaylist.list").Where(s => s != "==").ToList();
for (int i = 0; i < sectionCharacters.Count; i++)
{
if (i % 2 == 0)
{
var lvi = new ListViewItem { Text = sectionCharacters[i] };
lvi.SubItems.Add(sectionCharacters[i + 1]);
listView1.Items.Add(lvi);
}
}
I have ListView that is dynamic and Columns name need to change dynamic so i create this:
var gridView = new GridView();
this.lvWorkers.View = gridView;
foreach(string c in columnName)
{
gridView.Columns.Add(new GridViewColumn
{
Header = c.Remove(0, 5),
DisplayMemberBinding = new Binding(c)
});
}
Now i want add Items to this list but there is no SubItems like in Win Forms. I find similar problems, the answer was to make class that is defined. Add Items to Columns in a WPF ListView
I need to add Items dynamic.
Win Forms which is not work.
foreach (OneStudentEvent e in oneEventList)
{
ListViewItem item = new ListViewItem(e.Indeks.ToString());
item.SubItems.Add(e.eventString);
...
lvWorkers.Items.Add(item);
}
EDIT
Now i remove DisplayMemberBinding = new Binding(c)
and add:
lvWorkers.ItemsSource = getList();
private ArrayList getList()
{
ArrayList data = new ArrayList();
for(int i=0; i<20; i++)
{
List<string> tempList = new List<string>();
for(int j = 0; j < 30; j++)
{
tempList.Add(j.ToString());
}
data.Add(tempList);
}
return data;
}
And i don't see string in list but word: (Collection). I know that this is my bad to show collection no single string, but i don't know hot to make it.
I have a listbox that is bound to a list of objects from the database. I have a secondary list that has less objects that I want to use it to mark as selected elements.
cell = new HtmlTableCell();
List<ClasaAutor> listaAutori = DataTableToClasaAutor(dal.CitesteTotiAutori());
List<ClasaAutor> listaAutoriPublicatie = DataTableToClasaAutor(dal.CitesteTotiAutoriUneiPublicatii(guidPublicatie));
ListBox list = new ListBox();
list.SelectionMode = ListSelectionMode.Multiple;
list.ID = "cbAutori";
list.DataSource = listaAutori;
list.DataTextField = "NumeComplet";
list.DataValueField = "GuidAutor";
list.DataBind();
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Value == autor.GuidAutor.ToString())
list.SelectedIndex = i;
}
}
cell.Controls.Add(list);
row.Cells.Add(cell);
The problem is that only my last element gets selected... why? How can I fix it?
My if is ok, it gets true 2 times...
Try this loop:
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
foreach (ListItem item in list.Items)
{
if (item.Value == autor.GuidAutor.ToString())
item.Selected = true;
}
}
the problem in semantics, SelectedIndex of list can hold only one value, this is not collection
however you could make list item selected by setting the Selected value of it to true
list.Items[i].Selected = list.Items[i].Value == autor.GuidAutor.ToString();
I have a ListBox in WPF with a DataBinded Xml Collection. I set the SelectionMode to Extended so the user can select multiple items. I have a RemoveItem command which iterates through the selecteditems and removes them from the list:
var selecteditems = this.SelectedItems;
for(int i = 0; i < selecteditems.Count; i++ )
{
ItemBox ouritem = (ItemBox)this.ItemContainerGenerator.ContainerFromItem(this.SelectedItems[i]);
XmlDataProvider prov = this.DataContext as XmlDataProvider;
XmlNode MainNode = prov.Document.SelectSingleNode("//MainNode");
MainNode.RemoveChild(selecteditems[i] as XmlNode);
}
The problem is that after the first item of a selection is deleted, the selection is cleared and the last item of the list is selected.
How can I keep the selection that I started with and make sure all the items are deleted?
How about the old 'take a copy first' approach?:
IList selectedItems = new List<YourDataType>();
foreach (YourDataType item in this.SelectedItems) selectedItems.Add(item);
for (int index = selectedItems.Count - 1; index >= 0; index--)
{
// remove each selected item here
}
Execute your loop in reverse iteration.
var selecteditems = this.SelectedItems;
for(int i = selecteditems.Count-1; i>=0; i-- )
{
ItemBox ouritem = (ItemBox)this.ItemContainerGenerator.ContainerFromItem(this.SelectedItems[i]);
XmlDataProvider prov = this.DataContext as XmlDataProvider;
XmlNode MainNode = prov.Document.SelectSingleNode("//MainNode");
MainNode.RemoveChild(selecteditems[i] as XmlNode);
}
In my project I am trying to add the SelectedItems of ListView control (Only Items not sub items) to the ListBox Control. After adding, the selected Items of the ListView Control should get deleted. I am very close but I think I am doing something wrong in my code which leaving single selected item in the ListView control.
ListView --> lvEmpDetails
ListBox --> lbxEmpName
I tried the below code:
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
lvEmpDetails.SelectedItems[intCount].Remove();
}
Please suggest.
Don't delete the items in the same loop you're iterating them. Add them to a list and delete them afterwards:
var itemsToRemove = new List<ListViewItem>();
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
itemsToRemove.Add(lvEmpDetails.SelectedItems[intCount]);
}
foreach (var item in itemsToRemove)
{
item.Remove();
}
You may do another way:
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
lvEmpDetails.SelectedItems[intCount].Remove();
//Every time remove item, reduce the index
intCount--;
}