日期:2014-05-16  浏览次数:20471 次

comboBox绑定数据库后SelectedIndexChanged事件出错

        comBox控件绑定数据库字段后,在窗体的Load事件之前就已经触发SelectedIndexChanged了,这时可以加一个标识,让其在Load事件之后激发。

bool flag;
        private void Education_Training_Load(object sender, EventArgs e)
        { 
            MyDBase DB = new MyDBase(DBUser.sserver, DBUser.DBName, DBUser.suser, DBUser.spasswd);         
            DataSet  DS = DB.GetRecordset("select distinct (year(date)) as date from education_train where PID=1");
            comboBox_Year.DataSource = DS.Tables[0];
            comboBox_Year.DisplayMember = "date";
            comboBox_Year.ValueMember = "date";
            flag = true;
            DB.DBClose();
        }
        private void comboBox_Year_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!flag) return;
            MyDBase DB = new MyDBase(DBUser.sserver, DBUser.DBName, DBUser.suser, DBUser.spasswd);
            int d1 = int.Parse(comboBox_Year.Text);
            int d2 = d1 + 1;
            DataSet DS = DB.GetRecordset("SELECT * FROM VIEW_EDUCATION_TRAIN where  培训时间 between '" + d1 + "' and '" + d2 + "' ");
            if (DB.ErrorCode())
            {
                MessageBox.Show(DB.ErrMessage(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            dataGridView2.DataSource = DS.Tables[0];
            DB.DBClose();
        }