首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

combobox数据无法删除掉解决方法

2012-03-17 
combobox数据无法删除掉先描述问题:我在做winform开发中,有一个button删除数据,但是删除之后,combobox里却

combobox数据无法删除掉
先描述问题:
我在做winform开发中,有一个button删除数据,但是删除之后,combobox里却还存在有,我到数据库中查询,发现数据库中已经删除掉该数据了.
我调试,在调试的过程中,获取到的新数据其实也已经是删除了.可是绑定到combobox上,该数据TMD还有.百思不得其解啊.

该情况,在我的listview控件上一模一样. 一模一样的情况.

数据库是Access2003的数据库

我贴上数据访问层的代码:

C# code
        //下面是获取Access数据连接的 在AccessHelper.cs类里                 /// <summary>        /// 获取Access数据库连接语句        /// </summary>        /// <returns></returns>        public static OleDbConnection GetConn()        {            System.Data.OleDb.OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\source\\Database\\DB.mdb;");            return cnn;        }        /// <summary>        /// 根据SQL命令返回数据DataTable数据表,        /// 可直接作为dataGridView的数据源        /// </summary>        /// <param name="SQL"></param>        /// <returns></returns>        public static DataTable SelectToDataTable(string SQL)        {            OleDbDataAdapter adapter = new OleDbDataAdapter();            OleDbCommand command = new OleDbCommand(SQL, GetConn());            adapter.SelectCommand = command;            DataTable Dt = new DataTable();            adapter.Fill(Dt);            return Dt;        }                 //下面是winform代码      //取出所有分类 并绑定到combobox中  combobox控件名是:  cmFatherType       private void GetFatherTypeCom(string isType)        {            //cmFatherType.Items.Clear();            string sql = "select typeId,typeName from type_Info where fatherId=0 and isTYpe=" + isType;            DataTable dt = AccessHelper.SelectToDataTable(sql);            cmFatherType.DisplayMember = "typename";            cmFatherType.ValueMember = "typeId";            cmFatherType.DataSource = dt;        }        /// <summary>        ///取出分类  下面是取出所有分类 并绑定到ListView中 控件的名字叫  lvFatherType        /// </summary>        private void GetType()        {            int typeId;            string typeName;            string addTime;            string userName;            AccessHelper helper = new AccessHelper();            try            {                string sql = "select t.*,u.userName from type_info t,user_info u where t.userId=u.userId and fatherId=0 and isType=1";                OleDbDataReader reader;                reader = helper.GetDataReader(sql);                lvFatherType.Items.Clear();                if (reader.HasRows)                {                    while (reader.Read())                    {                        typeId = (int)reader["typeId"];                        typeName = (string)reader["typeName"];                        userName = (string)reader["userName"];                        addTime = Convert.ToDateTime(reader["addTime"]).ToString("yyyy年M月d号");                        //创建一个ListView选项                        ListViewItem lv = new ListViewItem(typeName);                        lv.Tag = typeId; ///将ID放在tag中                        lvFatherType.Items.Add(lv);//向ListView中添加一个新项                        lv.SubItems.AddRange(new string[] { userName, addTime });                    }                    reader.Close();                }            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            finally            {                helper.CloseDataBase();//关闭数据库            }        }         //下面是删除的代码  if (lvFatherType.SelectedItems.Count != 0)            {                ListView.SelectedIndexCollection c = lvFatherType.SelectedIndices;                string typeId = lvFatherType.Items[c[0]].Tag.ToString();                if (MessageBox.Show("您确定要删除 " + lvFatherType.Items[c[0]].SubItems[0].Text + " 吗?\n删除后将不可再恢复哦!", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)                {                                         if (AccessHelper.ExecuteDelete(" delete from type_info where typeId=" + typeId + " and isType=1 "))                        {                            //  lvFatherType.Items[lvFatherType.SelectedItems[0].Index].Remove();                            GetFatherTypeCom("1");                            GetFatherType();//重新显示分类                        }                        else                        {                            MessageBox.Show("删除失败,请重试!", "删除提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        }                                   }            }            else            {                MessageBox.Show("请选择您要删除的名称!", "修改提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            } 






我是实在没有办法了,才来问各位.这个鬼问题我弄了好几天了.真的,好几天了.百思不得其解啊!!

所以才来求助各位的.


关键是我调试的删除后 调试的时候 该数据已经不存在了,但绑定上去 却还有.

十分感谢.

[解决办法]
你看下你的方法之间的调用有没有问题,,打断点调试,看的返回的DataTable中是否和数据库中一致?
以前用Oracle数据库遇到和你一样的问题,,删除数据后没提交,删除的数据还是现实了,,但是Access中应该不会有这问题。。。
[解决办法]
我调试,在调试的过程中,获取到的新数据其实也已经是删除了.可是绑定到combobox上


获取到的新数据已经删除了,那绑定到combobox上还有,你绑定前清空一下,绑定到combobox后再绑定到一个新的控件上,看最后两个控件显示是否一样

热点排行