Here is your solution
For that I'm creating one class, you can get the details from database....
class Population
{
public string CountryName { get; set; }
public int TotalPopulation { get; set; }
public List<Population> GetPopulation()
{
List<Population> obj = new List<Population>();
obj.Add(new Population{CountryName="Select All",TotalPopulation=0 });
obj.Add(new Population{CountryName="country 1",TotalPopulation= 100});
obj.Add(new Population{CountryName="country 2",TotalPopulation= 95 });
obj.Add(new Population{CountryName="country 3",TotalPopulation= 90 });
obj.Add(new Population{CountryName="country 4",TotalPopulation= 80 });
return obj;
}
}
Write this code in form load
comboBox1.DataSource = new Population().GetPopulation();
comboBox1.DisplayMember = "CountryName";
comboBox1.ValueMember = "TotalPopulation";
Now Create Selected Index change event of comboBox
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(comboBox1.SelectedValue) == 0)
{
int total = 0;
for (int i = 0; i < comboBox1.Items.Count; i++)
{
comboBox1.Select();
total += Convert.ToInt32(comboBox1.SelectedValue);
}
label1.Text = total.ToString();
}
}
OR
you can do like this in selected index change event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(comboBox1.SelectedValue) == 0)
{
int total = 0;
List<Population> temp = (List<Population>)comboBox1.DataSource;
foreach (var item in temp)
{
total += item.TotalPopulation;
}
label1.Text = total.ToString();
}
}