Example #1
0
        private void btnUpdate_Click(object sender, EventArgs e) //更新此项成果
        {
            if (MyDatabase.TestMyDatabaseConnect() == false)     //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            frmAchievementUpdate achievement_update = new frmAchievementUpdate(txt_achievement_name); //实例化成果更新窗体

            achievement_update.ShowDialog();                                                          //成果更新窗体以模式对话框的方式打开

            #region 更新DataGridView控件内的内容
            try
            {
                DataTable dt = MyDatabase.GetDataSetBySql(sql_backup);
                if (dt == null)                             //数据库连接失败
                {
                    btnDelete.Enabled       = false;        //删除此项成果按钮不可点击
                    btnUpdate.Enabled       = false;        //更新此项成果按钮不可点击
                    btnOutToFile.Text       = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                    btnOutToFile.Enabled    = false;        //导出此检索成果到文件按钮不可点击
                    txt_achievement_name    = string.Empty; //记录数据库内主键的值为空
                    sql_backup              = string.Empty;
                    this.dgvData.DataSource = null;         //DataGridView控件显示数据
                    MessageBox.Show("数据库连接失败!", "提示");
                    return;
                }
                if (dt.Rows.Count == 0)                     //未检索到符合要求的成果信息
                {
                    btnDelete.Enabled       = false;        //删除此项成果按钮不可点击
                    btnUpdate.Enabled       = false;        //更新此项成果按钮不可点击
                    btnOutToFile.Text       = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                    btnOutToFile.Enabled    = false;        //导出此检索成果到文件按钮不可点击
                    txt_achievement_name    = string.Empty; //记录数据库内主键的值为空
                    sql_backup              = string.Empty;
                    this.dgvData.DataSource = null;         //DataGridView控件显示数据
                    return;
                }
                btnDelete.Enabled         = true;         //删除此项成果按钮可点击
                btnUpdate.Enabled         = true;         //更新此项成果按钮可点击
                btnOutToFile.Text         = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                btnOutToFile.Enabled      = true;         //导出此检索成果到文件按钮可点击
                this.dgvData.DataSource   = null;         //先置为null,否则在右键删除DataGridView后再次检索后显示的字段顺序会有误
                this.dgvData.DataSource   = dt;           //DataGridView控件显示数据
                this.txt_achievement_name = this.dgvData.Rows[0].Cells[0].Value.ToString().Trim();
                return;
            }
            catch (Exception ex)
            {
                btnDelete.Enabled       = false;        //删除此项成果按钮不可点击
                btnUpdate.Enabled       = false;        //更新此项成果按钮不可点击
                btnOutToFile.Text       = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                btnOutToFile.Enabled    = false;        //导出此检索成果到文件按钮不可点击
                txt_achievement_name    = string.Empty; //记录数据库内主键的值为空
                sql_backup              = string.Empty;
                this.dgvData.DataSource = null;         //DataGridView控件显示数据
                return;
            }
            #endregion
        }
        private void btnUpdate_Click(object sender, EventArgs e) //更新此项成果
        {
            if (MyDatabase.TestMyDatabaseConnect() == false)     //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            frmAchievementUpdate achievement_update = new frmAchievementUpdate(txt_achievement_name); //实例化成果更新窗体

            achievement_update.ShowDialog();                                                          //成果更新窗体以模式对话框的方式打开
            #region 成果类型组合框更新
            if (MyDatabase.TestMyDatabaseConnect() == false)                                          //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            try
            {
                this.cboAchievementType.Items.Clear();      //清空combox内所有选项
                DataTable dt_1 = MyDatabase.GetDataTableBySql("select distinct AchievementType from datainfo;");
                if (dt_1.Rows.Count == 0)                   //数据库中已无任何成果类型
                {
                    btnDelete.Enabled       = false;        //删除此项成果按钮不可点击
                    btnUpdate.Enabled       = false;        //更新此项成果按钮不可点击
                    btnOutToFile.Text       = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                    btnOutToFile.Enabled    = false;        //导出此检索成果到文件按钮不可点击
                    txt_achievement_name    = string.Empty; //记录数据库内主键的值为空
                    txt_achievement_type    = string.Empty;
                    this.dgvData.DataSource = null;         //DataGridView控件显示数据
                    return;
                }
                for (int i = 0; i < dt_1.Rows.Count; i++)
                {
                    this.cboAchievementType.Items.Add(MyDatabase.CharFilter(dt_1.Rows[i][0].ToString().Trim()));
                }
                bool checkresult = false;
                for (int i = 0; i < this.cboAchievementType.Items.Count; i++)
                {
                    if (string.Compare(cboAchievementType.GetItemText(cboAchievementType.Items[i]), txt_achievement_type) == 0)//成果类型combox中还有之前的成果类型
                    {
                        this.cboAchievementType.SelectedIndex = i;
                        checkresult = true;
                        break;
                    }
                }
                if (checkresult == false)//成果类型combox中已无之前的成果类型
                {
                    this.cboAchievementType.SelectedIndex = 0;
                    btnDelete.Enabled    = false;        //删除此项成果按钮不可点击
                    btnUpdate.Enabled    = false;        //更新此项成果按钮不可点击
                    btnOutToFile.Text    = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                    btnOutToFile.Enabled = false;        //导出此检索成果到文件按钮不可点击
                    txt_achievement_name = string.Empty; //记录数据库内主键的值为空
                    txt_achievement_type = string.Empty;
                    dgvData.DataSource   = null;
                    return;
                }
            }
            catch (Exception ex)//数据库可能连接不上,MyDatabase.GetDataSetBySql函数会出错,返回的DataSet值为null,导致下面引用DataSet具体值时会抛出异常(后经改进在DataSet具体值使用之前加入了值是否为null的判断,后又经过改进在每次事件处理前均加入了检测能否成功连接的函数)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            #region DataGridView控件内容更新
            try
            {
                string    sql_2 = string.Format("select datainfo.AchievementName as '成果名称', datainfo.AchievementType as '成果类型', datainfo.AchievementDate as '时间', datainfo.AchievementAuthor as '作者', datainfo.AchievementCompany as '单位', datainfo.AchievementMoney as '支撑基金' from datainfo where AchievementType='{0}';", txt_achievement_type);
                DataTable dt_2  = MyDatabase.GetDataTableBySql(sql_2);
                this.dgvData.DataSource = dt_2;//DataGridView控件显示数据
                txt_achievement_name    = MyDatabase.CharFilter(this.dgvData.Rows[0].Cells[0].Value.ToString().Trim());
                txt_achievement_type    = MyDatabase.CharFilter(this.dgvData.Rows[0].Cells[1].Value.ToString().Trim());
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据更新出错!");
                return;
            }
            #endregion
        }
        private void btnUpdate_Click(object sender, EventArgs e) //更新此项成果
        {
            if (MyDatabase.TestMyDatabaseConnect() == false)     //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            frmAchievementUpdate achievement_update = new frmAchievementUpdate(txt_achievement_name); //实例化成果更新窗体

            achievement_update.ShowDialog();                                                          //成果更新窗体以模式对话框的方式打开

            #region 更新DataGridView控件内的内容
            try
            {
                DataSet ds = MyDatabase.GetDataSetBySql(sql_backup);
                if (ds == null)                                //数据库连接失败
                {
                    btnDelete.Enabled       = false;           //删除此项成果按钮不可点击
                    btnUpdate.Enabled       = false;           //更新此项成果按钮不可点击
                    btnOutToExcel.Text      = "导出此检索成果到Excel"; //导出此检索成果到Excel按钮显示内容
                    btnOutToExcel.Enabled   = false;           //导出此检索成果到Excel按钮不可点击
                    txt_achievement_name    = string.Empty;    //记录数据库内主键的值为空
                    sql_backup              = string.Empty;
                    this.dgvData.DataSource = null;            //DataGridView控件显示数据
                    MessageBox.Show("数据库连接失败!", "提示");
                    return;
                }
                if (ds.Tables[0].Rows.Count == 0)              //未检索到符合要求的成果信息
                {
                    btnDelete.Enabled       = false;           //删除此项成果按钮不可点击
                    btnUpdate.Enabled       = false;           //更新此项成果按钮不可点击
                    btnOutToExcel.Text      = "导出此检索成果到Excel"; //导出此检索成果到Excel按钮显示内容
                    btnOutToExcel.Enabled   = false;           //导出此检索成果到Excel按钮不可点击
                    txt_achievement_name    = string.Empty;    //记录数据库内主键的值为空
                    sql_backup              = string.Empty;
                    this.dgvData.DataSource = null;            //DataGridView控件显示数据
                    return;
                }
                btnDelete.Enabled                = true;            //删除此项成果按钮可点击
                btnUpdate.Enabled                = true;            //更新此项成果按钮可点击
                btnOutToExcel.Text               = "导出此检索成果到Excel"; //导出此检索成果到Excel按钮显示内容
                btnOutToExcel.Enabled            = true;            //导出此检索成果到Excel按钮可点击
                this.dgvData.AutoGenerateColumns = true;            //自动
                this.dgvData.DataSource          = ds.Tables[0];    //DataGridView控件显示数据
                this.dgvData.Columns[0].ReadOnly = true;            //设为只读
                this.dgvData.Columns[1].ReadOnly = true;            //设为只读
                this.dgvData.Columns[2].ReadOnly = true;            //设为只读
                this.dgvData.Columns[3].ReadOnly = true;            //设为只读
                this.dgvData.Columns[4].ReadOnly = true;            //设为只读
                this.dgvData.Columns[5].ReadOnly = true;            //设为只读
                this.txt_achievement_name        = this.dgvData.Rows[0].Cells[0].Value.ToString().Trim();
                return;
            }
            catch (Exception ex)
            {
                btnDelete.Enabled       = false;           //删除此项成果按钮不可点击
                btnUpdate.Enabled       = false;           //更新此项成果按钮不可点击
                btnOutToExcel.Text      = "导出此检索成果到Excel"; //导出此检索成果到Excel按钮显示内容
                btnOutToExcel.Enabled   = false;           //导出此检索成果到Excel按钮不可点击
                txt_achievement_name    = string.Empty;    //记录数据库内主键的值为空
                sql_backup              = string.Empty;
                this.dgvData.DataSource = null;            //DataGridView控件显示数据
                return;
            }
            #endregion
        }