combobox数据无法删除掉
先描述问题:
我在做winform开发中,有一个button删除数据,但是删除之后,combobox里却还存在有,我到数据库中查询,发现数据库中已经删除掉该数据了.
我调试,在调试的过程中,获取到的新数据其实也已经是删除了.可是绑定到combobox上,该数据TMD还有.百思不得其解啊.
该情况,在我的listview控件上一模一样. 一模一样的情况.
数据库是Access2003的数据库
我贴上数据访问层的代码:
//下面是获取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); }