Mr_Green
Mr_Green

Reputation: 41832

Select only one Item from listView

I am beginner in c#. I need to show only one Item of the ListView (with its sub items), when user selects an item name which are populated also in ComboBox.

I know the event to use i.e., SelectedIndexChanged event. But I dont know what to do in that.

Currently I am using alternative big process which includes XML file and dataset which makes my project more complex.

The ListView has many items in it. Same item names are also in ComboBox.

If you have time please take a look at my very small project. download

EDIT: Here "Show only one item" means strictly show only one item, not select the particular item and show all the items.

Upvotes: 3

Views: 3204

Answers (2)

Mr_Green
Mr_Green

Reputation: 41832

After trying many attempts I settled with using DataSet or DataTable because here we need to save the changes made to the list view items and those changes should be saved in DataSet or DataTable so that those items can recall later.

Upvotes: 0

Furqan Safdar
Furqan Safdar

Reputation: 16698

Why don't you just use this piece of code to get the desired results:

ADDED:

Follow these steps:

  1. Create a Country class as follows:

    class Country
    {
        public string Name { get; set; }
        public string Flag { get; set; }
        public string Continent { get; set; }
        public string Capital { get; set; }
        public string Population { get; set; }
        public string Currency { get; set; }
    }
    
  2. Populate the countries information as per your requirement, as shown below:

    List<Country> countryList = new List<Country>() { 
        new Country() { Name = "India", Capital = "Delhi", Continent = "Asia", Currency = "Rupee", Population = "1.2 Billion", Flag = "india.gif" }, 
        new Country() { Name = "Pakistan", Capital = "Islamabad", Continent = "Asia", Currency = "Rupee", Population = "0,5 Billion", Flag = "pakistan.gif" }, 
        new Country() { Name = "Sri Lanka", Capital = "Kotte", Continent = "Asia", Currency = "SriLankan Rupee", Population = "20277597", Flag = "sri_lanka.gif" } 
    };
    
  3. Modify *frmFlag_Load* event as shown below:

    private void frmFlag_Load(object sender, EventArgs e)
    {
        lvMain.Items.Clear();
        tlstrpcmbCountries.Items.Clear();
    
        tlstrpcmbCountries.Items.Add("All");
        for (int i = 0; i < countryList.Count; i++)
        {
            tlstrpcmbCountries.Items.Add(countryList[i].Name);
        }
    
        tlstrpcmbCountries.SelectedIndex = 0;
        tlstrpcmbViews.SelectedIndex = 0;
    }
    
  4. Modify the SelectedIndexChanged event of your ComboBox and ListView as shown below:

    private void tlstrpcmbCountries_SelectedIndexChanged(object sender, EventArgs e)
    {
        var country = countryList.Where(c => c.Name.Equals(tlstrpcmbCountries.SelectedItem.ToString())).Select(s => s).FirstOrDefault();
    
        if (country != null)
        {
            lvMain.Items.Clear();
    
            ListViewItem item = new ListViewItem(country.Name, country.Flag);
    
            item.SubItems.Add(country.Continent);
            item.SubItems.Add(country.Capital);
            item.SubItems.Add(country.Population);
            item.SubItems.Add(country.Currency);
            lvMain.Items.Add(item);
    
            lvMain.EnsureVisible(0);
            item.Selected = true;
            item.Focused = true;
            lvMain.Select();
        }
    }
    
    private void lvMain_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lvMain.SelectedItems.Count > 0)
        {
            var selected = lvMain.SelectedItems[0];
            lblCountryName.Text = selected.SubItems[0].Text;
            lblContinent.Text = selected.SubItems[1].Text;
            lblCapitalCity.Text = selected.SubItems[2].Text;
            lblPopulation.Text = selected.SubItems[3].Text;
            lblCurrencyName.Text = selected.SubItems[4].Text;
        }
    }
    

By following this approach, you don't even need to match the order your items.

Happy Coding...

Upvotes: 5

Related Questions