Question : Easiest way to edit a row of data in ListView using C-Sharp

Hi,

I'm reacquainting myself with C# after a quick foray a few years ago and am playing around with ListView.  In this ListView, I am displaying nine columns of data.

I would like to know what is the best way (in C#) to allow someone to edit a row of data in ListView by double-clicking it.  I am fine with creating a new windows form for this specific task if needed, but am not interested in replacing my listview with a datagridview unless absolutely necessary for this task.

Also, I'm looking for an answer in actual C# code or psuedocode (with .Net library references.) as I learn better this way.  

Thanks,
- Dan

Answer : Easiest way to edit a row of data in ListView using C-Sharp

Hi,

The easiest way is to get the listview Item, and pass it into a Windows Form for editing.
On the edit form, when they click 'Ok' or save, set the ListViewItem values to the new values, this should automatically update the listview control.

The code below demonstrates how to 'capture' the double-clicked ListViewItem.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
/// 
        /// ListView1 Mouse Double Click Event
        /// 
        /// 
        /// 
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Get ListViewItem
            ListViewItem lvi = listView1.GetItemAt(e.X, e.Y);
 
            // If Item Exists at this point
            if (lvi != null)
            {
                // Do Anything with the ListView Item Selected, such as passing values to edit form etc.
                Form2 f = new Form2(lvi);
                f.ShowDialog();
            }
        }
Random Solutions  
 
programming4us programming4us