| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2856 人关注过本帖
标题:comboBox加载数据重复了怎么办
只看楼主 加入收藏
hao_t2010
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-7-6
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:3 
comboBox加载数据重复了怎么办
我做了一个comboBox联动,现在通过第一个comboBox中的选项,第二个comboBox可以显示相对应的数据,可是再通过第一个comboBox选择的时候,第二个comboBox中的数据是上一次的加上这一次的,怎么可以去掉上一次的数据,每次都显示当前这回选择的?
还有我想在加载的时候出现==请选择==,可是也没显示?
 private void dataExcel_Load(object sender, EventArgs e)
        {
            
            db.RunSQL("select goodsoffer from goodsInfo group by goodsoffer", ref ds, "goodsInfo");
            DataTable dtGroup = ds.Tables["goodsInfo"];
            comboBox1.DataSource = dtGroup;
            comboBox1.DisplayMember = "goodsOffer";

            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox1.SelectedItem;
                string gId = drv.Row["goodsOffer"].ToString();
                db.RunSQL("select goodsID,goodsBrand from goodsInfo where goodsoffer='" + gId + "'", ref ds, "goodsInfo1");

                DataTable dtUser = ds.Tables["goodsInfo1"];
                comboBox2.DataSource = dtUser;
                comboBox2.DisplayMember = "goodsBrand";
            }
        }
搜索更多相关主题的帖子: 加载 comboBox 数据 
2010-07-14 13:29
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
收藏
得分:10 
将:
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox1.SelectedItem;
                string gId = drv.Row["goodsOffer"].ToString();
                db.RunSQL("select goodsID,goodsBrand from goodsInfo where goodsoffer='" + gId + "'", ref ds, "goodsInfo1");

                DataTable dtUser = ds.Tables["goodsInfo1"];
                comboBox2.DataSource = dtUser;
                comboBox2.DisplayMember = "goodsBrand";
            }
        }
改为:
       private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dtUser=null;
            comboBox2.DataSource = dtUser;

            if (comboBox1.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox1.SelectedItem;
                string gId = drv.Row["goodsOffer"].ToString();
                db.RunSQL("select goodsID,goodsBrand from goodsInfo where goodsoffer='" + gId + "'", ref ds, "goodsInfo1");

                dtUser = ds.Tables["goodsInfo1"];
                comboBox2.DataSource = dtUser;
                comboBox2.DisplayMember = "goodsBrand";
            }
        }

你测试一下
2010-07-14 14:01
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
收藏
得分:0 
不行的话,再改为以下试试:
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dtUser=New DataTable();
            comboBox2.DataSource = dtUser;

            if (comboBox1.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox1.SelectedItem;
                string gId = drv.Row["goodsOffer"].ToString();
                db.RunSQL("select goodsID,goodsBrand from goodsInfo where goodsoffer='" + gId + "'", ref ds, "goodsInfo1");

                dtUser = ds.Tables["goodsInfo1"];
                comboBox2.DataSource = dtUser;
                comboBox2.DisplayMember = "goodsBrand";
            }
        }
2010-07-14 14:23
hao_t2010
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-7-6
收藏
得分:0 
感谢hainandgm,清空数据我已经搞定了,我用的是ds.Tables.Remove("goodsInfo1");
现在就差加一个"==请选择=="了
2010-07-14 14:32
快速回复:comboBox加载数据重复了怎么办
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020713 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved