C#: How to Edit Items and Subitems in a Listview

How to edit items in a listview

Since you want to use separate edit controls and buttons to update there is no need to overlay the listview subitems with controls.

Here is example code to load the controls from the 1st selected item and to update that Item in the ListView:

    private void lv_edit_SelectedIndexChanged(object sender, EventArgs e)
{
if (lv_edit.SelectedIndices.Count > 0)
{
var lvi = lv_edit.SelectedItems[0];
tb_col1.Text = lvi.SubItems[0].Text;
date_col2.Value = Convert.ToDateTime(lvi.SubItems[1].Text);
combo_col3.SelectedIndex = combo_col3.FindStringExact(lvi.SubItems[2].Text);
}
}

private void cb_updateItem_Click(object sender, EventArgs e)
{
if (lv_edit.SelectedIndices.Count > 0)
{
var lvi = lv_edit.SelectedItems[0];
lvi.SubItems[0].Text = tb_col1.Text;
lvi.SubItems[1].Text = date_col2.Value.ToString("dddd, dd. MMMM yyyy");
lvi.SubItems[2].Text = combo_col3.SelectedItem.ToString();
}
}

Note that SubItems[0].Text is the same as Items[0].Text.

Also note that the code assumes that all items have all three fields and their values are all valid, ie that the conversion and the find will work.

Making a certain listView sub-item column editable

The problem is you really don't what column you are in. I can think of a couple of ways to solve this:

1) Implement the code from this article to get the column number and test that: http://csharphelper.com/blog/2014/09/find-the-listview-row-and-column-under-the-mouse-in-c/

2) Set the subitem tag to true or false to determine edit-ability, then check the tag on the hit test:

var item1 = new ListViewItem(new[] { "i123", "Joe", "55" });

for (int i=0; i<3; i++)
{
if (i == 1)
item1.SubItems[i].Tag = true;
else
item1.SubItems[i].Tag = false;
}

Then for hit test:

if (SelectedLSI == null || (bool)SelectedLSI.Tag == false)
return;

I like the subitem tag method myself...

How to edit an Item in a ListView?

There are several methods to accomplish this task.

Instead of adding each item in list. You can use

EntreesListView.ItemSource = selectName;
EntreesListView.Refresh();

now your answer, how to get or modify 5th or any element
on some event when you get index of the element which is clicked

//get the item from original list
var obj = selectName[index];
//make changes to your object and again change the ItemSource
EntreesListView.ItemSource = selectName;
EntreesListView.Refresh();

Edit listview item based on click_event c#

Poking values into the UI is not a good design. I have these changes from my previous comments. Use a BindingList, public class Product : INotifyPropertyChanged, and replace the ListView with a DataGridView. This just makes life a whole lot easier and the code so simple. Creating ListBoxItems on your own is NOT a good way of doing things.
BTW: to find a product you just use a LINQ query on the ProductCollection. No searching the UI.

Select subitem in Listview and change value

If you want to select the whole row when subitem was clicked, try to use FullRowSelect property of ListView.
To handle double-click on a subitem, try this:

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
// Use hit.Item
// Use hit.SubItem
}

If you want allow end-user to edit subitem's text at the listview, I'm afraid the easiest way is to use Grid control. Alternative way is to try something like this:

private readonly TextBox txt = new TextBox { BorderStyle = BorderStyle.FixedSingle, Visible = false };

public Form1()
{
InitializeComponent();
listView1.Controls.Add(txt);
listView1.FullRowSelect = true;
txt.Leave += (o, e) => txt.Visible = false;
}

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);

Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
txt.Bounds = new Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, rowBounds.Width - leftMargin - 1, rowBounds.Height);
txt.Text = hit.SubItem.Text;
txt.SelectAll();
txt.Visible = true;
txt.Focus();
}


Related Topics



Leave a reply



Submit