组合框选择问题 [英] Combo box Selecting Issue

查看:59
本文介绍了组合框选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格和一个组合框,在那个组合中有4个国家.一旦我选择了国家,它就会在标签中显示该国家的总人口.我在组合框中需要一个全部"选项,当我选择全部"选项时,它必须显示4个国家的总人口.

我正在使用Access数据库,并且有一个表调用Country

数据库-国家

名字人口
国家1100
国家2 95
国家3120
国家4165

在这种情况下,请任何人都可以提供帮助.
谢谢.


请任何人都可以帮助我....

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

推荐答案

这是您的解决方案

为此,我要创建一个类,您可以从数据库中获取详细信息....

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;
    }

}



以load形式编写此代码



Write this code in form load

comboBox1.DataSource = new Population().GetPopulation();
comboBox1.DisplayMember = "CountryName";
comboBox1.ValueMember = "TotalPopulation";


现在,创建comboBox的Selected Index change事件


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);
        }
        //now assign the value in label
        label1.Text = total.ToString(); 
    }
}


OR


您可以在选定的索引更改事件中这样做


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();
    }
}


填充表时,在查询中将ALL添加为union子句.
Select countryname, id from Country UNION all, 0说.

然后在组合选择的项目事件上,编写逻辑以根据需要填充所有国家/地区的值.
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.


我已经为您提供了上一个问题的答案(AppendDataBoundItems选项). Clickety [
Already I gave you answer for your previous question(AppendDataBoundItems option). Clickety[^]

You didn''t reply to my comment.


这篇关于组合框选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆