Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 284
Mr_Green Avatar asked Dec 03 '25 17:12

Mr_Green


1 Answers

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...

like image 123
Furqan Safdar Avatar answered Dec 06 '25 06:12

Furqan Safdar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!