Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:29:54+00:00 2026-06-05T00:29:54+00:00

I have a ListView which displays multiple rows of ListViewItems. The user is able

  • 0

I have a ListView which displays multiple rows of ListViewItems. The user is able to edit one of these items through a dialog which opens after clicking ‘Edit.’ When the dialog closes I would like to modify the selected ListViewItem such that it reflects the new settings.

Here is how I currently update my item:

private void btnEditSnmpV3Setting_Click(object sender, EventArgs e)
{
    if (lstVwSNMPv3Settings.SelectedItems.Count > 0)
    {
        ListViewItem selectedItem = lstVwSNMPv3Settings.SelectedItems[0];
        NetworkDiscoverySnmpSetting settings = (NetworkDiscoverySnmpSetting)selectedItem.Tag;
        NetworkDiscoverySnmpV3SettingsDialog dialog = new NetworkDiscoverySnmpV3SettingsDialog(settings);

        //Pass in the owner for centering of dialog.
        if (dialog.ShowDialog(this) == DialogResult.OK)
        {
            selectedItem.SubItems.Clear();
            selectedItem.Text = settings.SnmpV3Username;
            selectedItem.SubItems.Add(settings.SecurityMode.ToString());
            selectedItem.SubItems.Add(settings.AuthenticationProtocol.ToString());
            selectedItem.SubItems.Add(settings.PrivacyProtocol.ToString());
            selectedItem.Tag = settings;
        }
    }
}

I found this to be a poor solution due to the fact that I need to touch code in multiple places if my ListView’s number of columns changes.

I handled this code-reuse issue during the ‘Add’ event (as opposed to ‘Edit’) by giving NetworkDiscoverySnmpSetting a utility method:

public ListViewItem ToListViewItem()
{
    ListViewItem listViewItem = new ListViewItem();
    listViewItem.Text = SnmpV3Username;
    listViewItem.SubItems.Add(SecurityMode.ToString());
    listViewItem.SubItems.Add(AuthenticationProtocol.ToString());
    listViewItem.SubItems.Add(PrivacyProtocol.ToString());
    listViewItem.Tag = this;
    return listViewItem;
}

which is used like so:

private void btnAddSnmpV3Setting_Click(object sender, EventArgs e)
{
    NetworkDiscoverySnmpSetting settings = new NetworkDiscoverySnmpSetting(NetworkDiscovery.ID);
    NetworkDiscoverySnmpV3SettingsDialog dialog = new NetworkDiscoverySnmpV3SettingsDialog(settings);
    //Pass in the owner for centering of dialog.
    if (dialog.ShowDialog(this) == DialogResult.OK)
        lstVwSNMPv3Settings.Items.Add(settings.ToListViewItem());
}

Unfortunately, ListView.SelectedItems does not allow collection-modification. As such, this does not compile:

lstVwSNMPv3Settings.SelectedItems[0] = settings.ToListViewItem();

How should I change my first code-snippet so that I do not need to update my code in multiple places when ListView’s columns change?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:29:56+00:00Added an answer on June 5, 2026 at 12:29 am

    You can modify the element itself rather than replacing it with another one, because ListViewItem is a class, so it’s a reference type.

    In order to do this follow these steps:

    • get currently selected item and save it to variable like this: ListViewItem selectedItem = lstVwSNMPv3Settings.SelectedItems[0];
    • modify your ToListViewItem method to void ToListViewItem(ListViewItem listViewItem) (return void and take ListViewItem object as parameter and modify it instead of creating a new object. It should also rather modify properties of existing subitems than creating new ones. It can look more or less like this:

      public void ToListViewItem(ListViewItem listViewItem)
      {
          listViewItem.Text = SnmpV3Username;
          listViewItem.SubItems[0].Text = SecurityMode.ToString();
          listViewItem.SubItems[1].Text = AuthenticationProtocol.ToString();
          listViewItem.SubItems[2].Text = PrivacyProtocol.ToString();
          listViewItem.Tag = this;
      }
      
    • call ToListViewItem(selectedItem);

    • you don’t have to assign the modified item back to the collection, because you use a reference, which means you’ve just modify the same object that’s in the ListView

    I did a quick test and the method seems to modify texts of existing items without issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview which displays items retrieved from the webpage. Each item in
I have a ListView which displays 5 listitems. When the user selects the last
I have two Activities in my App, one is a ListView which displays a
I have a ListView which contains custom rows. This custom row has following UI
I have a listview in which a user adds his comments on a particular
I have an action Edit in my WPF application, which is bound to items
I have a listview, which displays a list of administrators, this list is held
I have a ListView-like control that displays a list of items of various heights.
I have a ListView which displays information about some entities (all of the same
I have a list (ListView) which which displays a lot of information, and what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.