Get selected Row in ListView - c#

I have a Listener on the DoubleClick event in a ListView. I have also activated FullRowSelect.
So when I double-click a row, only the value in the first column appears. I also tried it directly with SelectedItems.
Please help
Code:
private void lvRecipesPos_DoubleClick(object sender, EventArgs e)
{
String s = "";
foreach (ListViewItem item in lvRecipesPos.Items)
{
if (item.Selected == true)
{
s += item.Text.ToString();
}
}
MessageBox.Show(s);
}

1) The ListView has a SelectedItems collection, so you don't have to iterate all items and check if they're selected.
2) Item has a SubItems collection which holds the texts for all sub items

Related

how to delete selected item in C#

I create a listview called listBoxobj. I want to delete the listview based on the selected listview but right now the problem is that when deleting it will delete by index not the selected item.
This is for an apps develops in visual studio,running SQLite.
i've already tried to use this code but keep failed
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
I want the selected listview will be deleted instead the listview deleted by index
private void DeleteMV_Click(object sender, RoutedEventArgs e)
{
DatabaseHelperMV_Meriam delete = new DatabaseHelperMV_Meriam();
x = listBoxobj.SelectedItems;//this line keep getting error
delete.DeleteId(x);
Frame.Navigate(typeof(MV_MeriamViewData));
}
For WinForms:
You delete list view selected item using this:
listBoxobj.SelectedItems[0].Remove();
This code you have:
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
Is corrected if you have multiselect = true... but if you have multiselect = false, use this: listBoxobj.SelectedItems[0].Remove();
For WPF:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = listBoxobj.SelectedItems.Cast<Object>().ToArray();
foreach (var item in selected)
{
listBoxobj.Items.Remove(item);
}
}

Add a row from the checkedlistbox to the DataGridView

I want to add an item to the DataGridView
If checked ListBox is checked. but add an item one time.Don't add it twice.
and I want the unchecked object in the checkedlistbox to be deleted as a row in the DataGridView. But my code is not working
Video
private void checkedListBox3_ItemCheck(object sender, ItemCheckEventArgs e)
{
foreach (int indexChecked in checkedListBox3.CheckedIndices)
{
if (checkedListBox3.GetItemCheckState(indexChecked) == CheckState.Checked)
{
dataGridView3.Rows.Add(checkedListBox3.SelectedItem.ToString());
}
else if (checkedListBox3.GetItemCheckState(indexChecked) == CheckState.Unchecked)
{
dataGridView3.Row[indexChecked].Delete;
}
}
}
The problem is that each time the checkedListBox event is fired, you loop through all the checked items and add them to the DGV, hence the duplications.
Instead, you can do this:
private void checkedListBox3_SelectedIndexChanged(object sender, ItemCheckEventArgs e)
{
int selectedIndex = checkedListBox3.SelectedIndex;
// If item is checked, add it to DGV
if (checkedListBox3.GetItemCheckState(selectedIndex) == CheckState.Checked)
{
dataGridView3.Rows.Add(checkedListBox3.Items[selectedIndex].ToString());
}
// If item is unchecked, search for its first occurence in the DGV and remove it
else if (checkedListBox3.GetItemCheckState(selectedIndex) == CheckState.Unchecked)
{
DataGridViewRow row = dataGridView3.Rows
.Cast<DataGridViewRow>()
.First(r => r.Cells["ENTER_YOUR_COLUMN_NAME_HERE"].Value.ToString().Equals(checkedListBox3.Items[selectedIndex].ToString()));
dataGridView3.Rows.Remove(row);
}
}
I would suggest a DerivedCollection. This would work like this:
You have a ReacvtiveList bound to your left panel with checkbox. Person class has to implement INotifyPropertyChanged of course and have Checked property.
Then you can do something like that:
CheckedPeople = People.CreateDerivedCollection(x => x, x => x.Checked);
This will create observable read-only collection storing exactly the same objects that your original list, but filtered by Checked property. Then you can bind CheckedPeople as ItemsSource to your grid and still edit them (only the collection is read only, since it's fed by changes in People collections).

How to show items from a listview group when selected from combobox

I am trying to display items in a listview from groups and here is what I mean.
I added a listview to a form and in the listview I added a 2 groups then I added items and for the items I chose a group name.
Now in a combox box I add in the selectindexchanged event I put this.
if (comboBox1.Text == "group1")
{
foreach (string itemname1 in listimages.Groups[0].Items)
{
string currentitem = itemname1;
}
}
nothing is working so I am trying to figure out what I am not doing right.
The items in the combobox have the same items as group names.
Any help would be great.
I was able to figure it out. Here is what I did
First on the listview I added Groups, then I added items and on the items in the Tag property I put in the group I wanted it to be linked to.
In the comboBox I put in group names as I typed them when I added them to the listview.
Then I added this code :
private void frmImageSelection_Load(object sender, EventArgs e)
{
items = new ListViewItem[listimages.Items.Count];
listimages.Items.CopyTo(items, 0);
ShowGroup(0);
cmbgroups.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowGroup(cmbgroups.SelectedIndex);
}
void ShowGroup(int index)
{
if (index == 0) // all
{
listimages.Items.Clear();
listimages.Items.AddRange(items);
}
else
{
listimages.Items.Clear();
foreach (ListViewItem item in items)
if (listimages.Groups[index].Name.Equals(item.Tag))
listimages.Items.Add(item);
}
foreach (ListViewItem item in listimages.Items)
item.Group = listimages.Groups[index];
}
ListViewItem[] items;
Anyway if you are not sure I will be glad to help you out just drop me a message in my inbox or something.

how to remove all uncheck items from listview c#

i need to remove all unchecked item from listview winforms c# in textchange event
for e.g i need to do like below
private void textBox_supplierName_TextChanged(object sender, EventArgs e)
{
if (listView_supplierNames.CheckedItems==CheckState.Unchecked)
{
// remove item
}
}
how to do it ...thanks
Use ListViewItem.Remove method to remove item from its associated ListView control:
foreach (ListViewItem item in listView_supplierNames.Items)
if (!item.Checked)
item.Remove();
Loop through the ListView Items and Use ListViewItem.Remove to Remove Items
foreach (ListViewItem item in listView_supplierNames.Items)
{
if (item.Checked)
{
}
else
{
//Remove unchecked Items
listView1.Items.Remove(item);
}
}
Get all the unchecked items and use Remove - example-
foreach(var item in listView.SelectedItems)
{
listView.Items.Remove(item)
}

How to remove ComboBox Item from Collection by pressing Del Key?

I'm trying to remove selected item from ComboBox Collection:
I wrote a buttonClick:
cb01.Items.Remove(cb01.SelectedItem);.
This deletes the item, but next time I open the Form - the item appears again.
Please help.
Add KeyDown event for your ComboBox and then
private void cb01_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if(cb01.SelectedIndex != -1)
cb01.Items.Remove(cb01.SelectedItem);
}
}
Above will remove items from comboBox but if you add items on designed time when you load the application again you can see all the items again.
check your InitializeComponent() method. you can see something like below.
this.cb01.Items.AddRange(new object[] {
"item1",
"item2",
"item13"});
when you load the application again, it will call InitializeComponent and call above method to add items.
To avoid this issue. You can use bound data source. e.g you can take items from database. and when you delete you can delete it from database. next time you load the application it only show the items in the database.
How about
if(cb01.SelectedItem != null)
cb01.Items.Remove(cb01.SelectedItem);
Why i did checking?
Since in last line you said
cb01.Items.RemoveAt(cb01.SelectedIndex); // error: Value of '-1' is not valid...
-1 is the index of combo when no item is selected. So i checked for selected item first. If found will go in if statement.
Replace comboBox1 with the name of your combobox and bind its KeyDown event
void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
int currentItem = comboBox1.SelectedIndex;
if (e.KeyCode == Keys.Delete && currentItem != -1)
{
comboBox1.Items.RemoveAt(currentItem);
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = (currentItem > 0 ? currentItem - 1 : currentItem);
}
}
This will select the next item in list after removing it, or do nothing if there are either no items in the comboBox or no item is selected.

Categories

Resources