Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form and a combo box, in that combo have 4 countries. Once i select the country it shows the total population of that country in a label. i need a "ALL" option in the combo box and when i select the "ALL" option it have to show total population of 4 countries.

I am using a Access Database and have a table call Country

database - Country

Name Population
Country 1 100
Country 2 95
Country 3 120
Country 4 165

Please anyone can help in this case.
Thanks.


Please any one can help me....
Posted
Updated 21-Jun-11 23:43pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:12pm    
Tag it properly. Forms? Tag it!
--SA

Here is your solution

For that I'm creating one class, you can get the details from database....

XML
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
MIDL
comboBox1.DataSource = new Population().GetPopulation();
comboBox1.DisplayMember = "CountryName";
comboBox1.ValueMember = "TotalPopulation";

Now Create Selected Index change event of comboBox
C#
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);
        }
        //now assign the value in label
        label1.Text = total.ToString(); 
    }
}

OR


you can do like this in selected index change event
C#
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();
    }
}
 
Share this answer
 
v2
When you populate the table, add ALL as a union clause in the query.
Select countryname, id from Country UNION all, 0 say.

Then on the combo selected item event, write logic to populate all country values as you want.
 
Share this answer
 
Comments
ruzan hizar 22-Jun-11 5:17am    
it comes error in query and asking to insert SELECT, INSERT OR DELETE
Already I gave you answer for your previous question(AppendDataBoundItems option). Clickety[^]

You didn't reply to my comment.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900