private void btnOutToFile_Click(object sender, EventArgs e) //导出此项成果到文件
 {
     if (MyDatabase.TestMyDatabaseConnect() == false)        //数据库连接失败
     {
         MessageBox.Show("数据库连接失败!", "提示");
         return;
     }
     if (txt_achievement_name != string.Empty)
     {
         try
         {
             DataTable dt = MyExcel.GetDgvToTable(dgvData);
             this.sfdOutToFile.Filter       = "Excel 工作簿(*.xlsx)|*.xlsx|文本文档(*.txt)|*.txt"; //设置保存类型
             this.sfdOutToFile.AddExtension = true;                                         //自动添加扩展名
             DialogResult result = this.sfdOutToFile.ShowDialog();
             if (result == DialogResult.OK)                                                 //点击了保存
             {
                 btnOutToFile.Text    = "正在导出中。。。";                                         //导出此项成果到文件按钮显示内容
                 btnOutToFile.Enabled = false;                                              //导出此项成果到文件按钮显示不可点击
                 if (MyData.DateTimeRemoveTime(dt) == false)                                //对dt内的数据进行单独处理,去除时间(默认值00:00:00),只保留日期
                 {
                     btnOutToFile.Text    = "导出此项成果到文件";                                    //设置导出此项成果到文件按钮显示内容
                     btnOutToFile.Enabled = true;                                           //设置导出此项成果到文件按钮显示可点击
                     MessageBox.Show("导出失败!", "提示");
                     return;
                 }
                 if (MyData.GetFileExtByFileName(sfdOutToFile.FileName) == "xlsx")     //导出到Excel文件
                 {
                     MyExcel.SaveDataToExcel(dt, sfdOutToFile.FileName);               //此过程很慢
                 }
                 else if (MyData.GetFileExtByFileName(sfdOutToFile.FileName) == "txt") //导出到txt文件
                 {
                     MyTxt.SaveDataToTxt(dt, sfdOutToFile.FileName);
                 }
                 else//扩展名有误
                 {
                     btnOutToFile.Text    = "导出此检索成果到文件";//设置导出此检索成果到文件按钮显示内容
                     btnOutToFile.Enabled = true;//设置导出此检索成果到文件按钮显示可点击
                     MessageBox.Show("导出类型为不支持的类型,导出失败!", "提示");
                     return;
                 }
                 btnOutToFile.Text    = "导出此项成果到文件"; //导出此项成果到文件按钮显示内容
                 btnOutToFile.Enabled = true;        //导出此项成果到文件按钮显示可点击
                 MessageBox.Show("导出成功!", "提示");
                 return;
             }
             else if (result == DialogResult.Cancel) //点击了取消
             {
                 btnOutToFile.Text    = "导出此项成果到文件"; //设置导出此项成果到文件按钮显示内容
                 btnOutToFile.Enabled = true;        //设置导出此项成果到文件按钮显示可点击
                 return;
             }
         }
         catch (Exception ex)
         {
             btnOutToFile.Text    = "导出此项成果到文件"; //设置导出此项成果到文件按钮显示内容
             btnOutToFile.Enabled = true;        //设置导出此项成果到文件按钮显示可点击
             MessageBox.Show("导出失败!", "提示");
             return;
         }
     }
     btnOutToFile.Text    = "导出此项成果到文件"; //设置导出此项成果到文件按钮显示内容
     btnOutToFile.Enabled = true;        //设置导出此项成果到文件按钮显示可点击
     MessageBox.Show("导出失败!", "提示");
 }
        private void btnAdd_Click(object sender, EventArgs e) //录入此项成果
        {
            if (MyDatabase.TestMyDatabaseConnect() == false)  //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            if (this.txtAchievementName.Text.ToString().Trim() == string.Empty || this.dtpAchievementDate.Text.ToString().Trim() == string.Empty || this.txtAchievementAuthor.Text.ToString().Trim() == string.Empty || this.txtAchievementCompany.Text.ToString().Trim() == string.Empty || this.txtAchievementMoney.Text.ToString().Trim() == string.Empty)//数据是否为空检测
            {
                MessageBox.Show("信息输入不完整,请重新检查之后再重试!", "提示");
                return;
            }
            if (string.Compare(this.cboAchievementType.SelectedItem.ToString(), "(其他)") == 0)//对成果类型数据是否为空进行单独判断
            {
                if (this.txtAchievementType.Text.ToString().Trim() == string.Empty)
                {
                    MessageBox.Show("信息输入不完整,请重新检查之后再重试!", "提示");
                    return;
                }
            }
            try                                                                                            //对成果时间和成果支撑基金的数据格式进行检测(如数据有误会throw异常,程序进入catch捕捉代码中)
            {
                Convert.ToDateTime(this.dtpAchievementDate.Text.ToString().Trim()).ToString("yyyy-MM-dd"); //最后加入ToString("yyyy-MM-dd")是为了去除时间只留下日期
                Convert.ToDouble(this.txtAchievementMoney.Text.ToString().Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("信息输入格式有误,请重新检查之后再重试!", "提示");
                //throw new Exception(ex.Message);
                return;
            }
            //所有数据的合法性均检测完毕
            string sql_1 = string.Empty;

            if (string.Compare(this.cboAchievementType.SelectedItem.ToString(), "(其他)") == 0)                                                                                                                                                                                                                                                                                                                                                                                     //对成果类型数据进行判断
            {
                sql_1 = string.Format("insert into datainfo values('{0}', '{1}', '{2}', '{3}', '{4}', {5});", this.txtAchievementName.Text.ToString().Trim(), this.txtAchievementType.Text.ToString().Trim(), Convert.ToDateTime(this.dtpAchievementDate.Text.ToString().Trim()).ToString("yyyy-MM-dd"), this.txtAchievementAuthor.Text.ToString().Trim(), this.txtAchievementCompany.Text.ToString().Trim(), Convert.ToDouble(this.txtAchievementMoney.Text.ToString().Trim())); //sql语句
            }
            else
            {
                sql_1 = string.Format("insert into datainfo values('{0}', '{1}', '{2}', '{3}', '{4}', {5});", this.txtAchievementName.Text.ToString().Trim(), this.cboAchievementType.SelectedItem.ToString().Trim(), Convert.ToDateTime(this.dtpAchievementDate.Text.ToString().Trim()).ToString("yyyy-MM-dd"), this.txtAchievementAuthor.Text.ToString().Trim(), this.txtAchievementCompany.Text.ToString().Trim(), Convert.ToDouble(this.txtAchievementMoney.Text.ToString().Trim())); //sql语句
            }
            if (MyDatabase.UpdateDataBySql(sql_1))                                                                                                                                                                                                                                                                                                                                                                                                                                        //添加成功
            {
                btnDelete.Enabled     = true;                                                                                                                                                                                                                                                                                                                                                                                                                                             //删除此项成果按钮可点击
                btnOutToExcel.Text    = "导出此项成果到Excel";                                                                                                                                                                                                                                                                                                                                                                                                                                   //导出此项成果到Excel按钮显示内容
                btnOutToExcel.Enabled = true;                                                                                                                                                                                                                                                                                                                                                                                                                                             //导出此项成果到Excel按钮可点击
                txt_achievement_name  = this.txtAchievementName.Text.ToString().Trim();                                                                                                                                                                                                                                                                                                                                                                                                   //记录数据库内主键的值
                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 AchievementName='{0}';", this.txtAchievementName.Text.ToString().Trim());
                DataSet ds    = MyDatabase.GetDataSetBySql(sql_2);
                if (ds == null)                               //数据库连接失败
                {
                    btnDelete.Enabled       = false;          //删除此项成果按钮不可点击
                    btnOutToExcel.Text      = "导出此项成果到Excel"; //导出此项成果到Excel按钮显示内容
                    btnOutToExcel.Enabled   = false;          //导出此项成果到Excel按钮不可点击
                    txt_achievement_name    = string.Empty;   //记录数据库内主键的值为空
                    this.dgvData.DataSource = null;           //DataGridView控件显示数据
                    MessageBox.Show("数据库连接失败!", "提示");
                    return;
                }
                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;         //设为只读
                MessageBox.Show("添加成功!", "提示");
            }
            else//添加失败
            {
                btnDelete.Enabled       = false;//删除此项成果按钮不可点击
                btnOutToExcel.Text      = "导出此项成果到Excel"; //导出此项成果到Excel按钮显示内容
                btnOutToExcel.Enabled   = false;          //导出此项成果到Excel按钮不可点击
                txt_achievement_name    = string.Empty;   //记录数据库内主键的值为空
                this.dgvData.DataSource = null;           //DataGridView控件显示数据
                MessageBox.Show("添加失败!", "提示");
            }
        }
Esempio n. 3
0
        private void frmMechanicalDrawing_Load(object sender, EventArgs e) //窗体载入时初始化
        {
            txtAchievementName.Text = string.Empty;                        //成果名称显示内容
            txtSaveDirectory.Text   = string.Empty;                        //成果保存目录显示内容
            txtPartNum.Text         = "0";                                 //零件图个数显示内容
            txtAssemblyNum.Text     = "0";                                 //装配图个数显示内容
            txtDrawingNum.Text      = "0";                                 //工程图纸个数显示内容
            this.dgvData.DataSource = null;                                //机械图信息显示DataGridView控件显示数据
            btnCount.Text           = "开始统计个数信息";                          //设置开始统计个数信息按钮显示内容
            btnCount.Enabled        = true;                                //开始统计个数信息按钮可点击
            btnOutToFile.Text       = "导出此视图内容到文件";                        //设置导出此视图内容到文件按钮显示内容
            btnOutToFile.Enabled    = false;                               //设置导出此视图内容到文件按钮不可点击
            btnUpdate.Enabled       = false;                               //更新机械图信息按钮不可点击

            #region 所用软件组合框初始化
            this.cboSoftware.Items.Clear();//清空combox内所有选项
            this.cboSoftware.Items.Add("CAD");
            this.cboSoftware.Items.Add("UG");
            this.cboSoftware.Items.Add("Solidworks");
            this.cboSoftware.Items.Add("Inventor");
            this.cboSoftware.Items.Add("ProE");
            this.cboSoftware.Items.Add("Catia");
            this.cboSoftware.Items.Add("(其他)");
            this.cboSoftware.Items.Add("(Unknown)"); //所用软件未知,数据库内该字段的默认值
            this.cboSoftware.SelectedIndex = 7;      //选中默认值"(Unknown)"
            #endregion

            if (MyDatabase.TestMyDatabaseConnect() == false)//数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }

            try
            {
                string  sql_1 = "select datainfo.AchievementName as '成果名称', datainfo.AchievementType as '成果类型', datainfo.AchievementDate as '时间', datainfo.AchievementAuthor as '作者', datainfo.AchievementCompany as '单位', datainfo.AchievementMoney as '支撑基金' from datainfo where AchievementType='机械图';";
                DataSet ds_1  = MyDatabase.GetDataSetBySql(sql_1);
                if (ds_1.Tables[0].Rows.Count == 0)         //未检索到任何机械图成果
                {
                    this.dgvData.DataSource = null;         //机械图信息显示DataGridView控件显示数据
                    btnUpdate.Enabled       = false;        //更新机械图信息按钮不可点击
                    btnOutToFile.Text       = "导出此视图内容到文件"; //设置导出此视图内容到文件按钮显示内容
                    btnOutToFile.Enabled    = false;        //设置导出此视图内容到文件按钮不可点击
                    MessageBox.Show("目前未有任何机械图成果,请先添加机械图成果!", "提示");
                    return;
                }
                for (int i = 0; i < ds_1.Tables[0].Rows.Count; i++)//对所有在mechanicaldrawing表中没有的机械图成果自动添加所有值为默认值的记录
                {
                    string  sql_2 = string.Format("select mechanicaldrawing.AchievementName as '成果名称', mechanicaldrawing.SaveDirectory as '成果保存目录', mechanicaldrawing.PartNum as '零件图个数', mechanicaldrawing.AssemblyNum as '装配图个数', mechanicaldrawing.DrawingNum as '工程图纸个数' from mechanicaldrawing where mechanicaldrawing.AchievementName='{0}';", ds_1.Tables[0].Rows[i][0].ToString().Trim());
                    DataSet ds_2  = MyDatabase.GetDataSetBySql(sql_2);
                    if (ds_2.Tables[0].Rows.Count == 0)//此机械图成果在mechanicaldrawing表无记录,添加所有值为默认值的记录
                    {
                        string sql_3 = string.Format("insert into mechanicaldrawing values('{0}', '(Unknown)', '(Null)', 0, 0, 0);", ds_1.Tables[0].Rows[i][0].ToString().Trim());
                        if (MyDatabase.UpdateDataBySql(sql_3) == false) //添加失败
                        {
                            this.dgvData.DataSource = null;             //机械图信息显示DataGridView控件显示数据
                            btnUpdate.Enabled       = false;            //更新机械图信息按钮不可点击
                            btnOutToFile.Text       = "导出此视图内容到文件";     //设置导出此视图内容到文件按钮显示内容
                            btnOutToFile.Enabled    = false;            //设置导出此视图内容到文件按钮不可点击
                            MessageBox.Show("获取数据初始化时失败!", "提示");
                            return;
                        }
                    }
                }
                string  sql_4 = "select datainfo.AchievementName as '成果名称', datainfo.AchievementType as '成果类型', datainfo.AchievementDate as '时间', datainfo.AchievementAuthor as '作者', datainfo.AchievementCompany as '单位', datainfo.AchievementMoney as '支撑基金', mechanicaldrawing.Software as '所用软件', mechanicaldrawing.SaveDirectory as '成果保存目录', mechanicaldrawing.PartNum as '零件图个数', mechanicaldrawing.AssemblyNum as '装配图个数', mechanicaldrawing.DrawingNum as '工程图纸个数' from datainfo join mechanicaldrawing on datainfo.AchievementName=mechanicaldrawing.AchievementName;";
                DataSet ds_4  = MyDatabase.GetDataSetBySql(sql_4);
                this.dgvData.DataSource      = ds_4.Tables[0]; //机械图信息显示DataGridView控件显示数据
                btnUpdate.Enabled            = true;           //更新机械图信息按钮可点击
                btnOutToFile.Text            = "导出此视图内容到文件";   //设置导出此视图内容到文件按钮显示内容
                btnOutToFile.Enabled         = true;           //设置导出此视图内容到文件按钮可点击
                this.txtAchievementName.Text = this.dgvData.Rows[0].Cells[0].Value.ToString().Trim();
                this.cboSoftware.Text        = this.dgvData.Rows[0].Cells[6].Value.ToString().Trim();
                this.txtSaveDirectory.Text   = this.dgvData.Rows[0].Cells[7].Value.ToString().Trim();
                this.txtPartNum.Text         = this.dgvData.Rows[0].Cells[8].Value.ToString().Trim();
                this.txtAssemblyNum.Text     = this.dgvData.Rows[0].Cells[9].Value.ToString().Trim();
                this.txtDrawingNum.Text      = this.dgvData.Rows[0].Cells[10].Value.ToString().Trim();
                return;
            }
            catch (Exception ex)
            {
                this.dgvData.DataSource = null;         //机械图信息显示DataGridView控件显示数据
                btnUpdate.Enabled       = false;        //更新机械图信息按钮不可点击
                btnOutToFile.Text       = "导出此视图内容到文件"; //设置导出此视图内容到文件按钮显示内容
                btnOutToFile.Enabled    = false;        //设置导出此视图内容到文件按钮不可点击
                MessageBox.Show("机械图管理界面初始化失败!", "提示");
                return;
            }
        }
Esempio n. 4
0
 private void btnSearch_Click(object sender, EventArgs e) //检索
 {
     if (MyDatabase.TestMyDatabaseConnect() == false)     //数据库连接失败
     {
         MessageBox.Show("数据库连接失败!", "提示");
         return;
     }
     try//防止将所有成果类型均删除后,成果类型为空时检索,会导致未引用的异常
     {
         if (this.cboAchievementType.SelectedItem.ToString() == string.Empty)
         {
             MessageBox.Show("成果类型选择有误", "提示");
             return;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("无任何待检索成果类型,检索失败!", "提示");
         return;
     }
     try
     {
         string    sql = 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}';", MyDatabase.CharFilter(this.cboAchievementType.SelectedItem.ToString().Trim()));
         DataTable dt  = MyDatabase.GetDataSetBySql(sql);
         if (dt == null)                             //数据库连接失败
         {
             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控件显示数据
             MessageBox.Show("数据库连接失败!", "提示");
             return;
         }
         if (dt.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控件显示数据
             MessageBox.Show("未检索到符合要求的成果信息!", "提示");
             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 = MyDatabase.CharFilter(this.dgvData.Rows[0].Cells[0].Value.ToString().Trim());
         this.txt_achievement_type = MyDatabase.CharFilter(this.dgvData.Rows[0].Cells[1].Value.ToString().Trim());
         MessageBox.Show("检索成功!", "提示");
         return;
     }
     catch (Exception ex)
     {
         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控件显示数据
         MessageBox.Show("检索失败!", "提示");
         return;
     }
 }
Esempio n. 5
0
        private void btnDelete_Click(object sender, EventArgs e) //删除此项成果
        {
            if (MyDatabase.TestMyDatabaseConnect() == false)     //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            DialogResult result = MessageBox.Show("是否确定删除此项成果?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (result == DialogResult.OK)//确定
            {
                if (txt_achievement_name != string.Empty)
                {
                    string sql_1 = string.Format("delete from datainfo where datainfo.AchievementName='{0}';", txt_achievement_name); //sql语句
                    if (MyDatabase.UpdateDataBySql(sql_1))                                                                            //删除成功
                    {
                        //更新删除后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.GetDataSetBySql(sql_2);
                            if (dt_2.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控件显示数据

                                #region 成果类型组合框更新
                                if (MyDatabase.TestMyDatabaseConnect() == false)//数据库连接失败
                                {
                                    MessageBox.Show("数据库连接失败!", "提示");
                                    return;
                                }
                                try
                                {
                                    this.cboAchievementType.Items.Clear();      //清空combox内所有选项
                                    DataTable dt_3 = MyDatabase.GetDataSetBySql("select distinct AchievementType from datainfo;");
                                    if (dt_3.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控件显示数据
                                        MessageBox.Show("删除成功!", "提示");
                                        return;
                                    }
                                    for (int i = 0; i < dt_3.Rows.Count; i++)
                                    {
                                        this.cboAchievementType.Items.Add(MyDatabase.CharFilter(dt_3.Rows[i][0].ToString().Trim()));
                                    }
                                    this.cboAchievementType.SelectedIndex = 0;
                                }
                                catch (Exception ex)//数据库可能连接不上,MyDatabase.GetDataSetBySql函数会出错,返回的DataSet值为null,导致下面引用DataSet具体值时会抛出异常(后经改进在DataSet具体值使用之前加入了值是否为null的判断,后又经过改进在每次事件处理前均加入了检测能否成功连接的函数)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                                #endregion
                                MessageBox.Show("删除成功!", "提示");
                                return;
                            }
                            btnDelete.Enabled       = true;         //删除此项成果按钮可点击
                            btnUpdate.Enabled       = true;         //更新此项成果按钮可点击
                            btnOutToFile.Text       = "导出此检索成果到文件"; //导出此检索成果到文件按钮显示内容
                            btnOutToFile.Enabled    = true;         //导出此检索成果到文件按钮可点击
                            this.dgvData.DataSource = null;         //先置为null,否则在右键删除DataGridView后再次检索后显示的字段顺序会有误
                            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());
                            MessageBox.Show("删除成功!", "提示");
                            return;
                        }
                        catch (Exception ex)
                        {
                            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控件显示数据
                            MessageBox.Show("删除失败!", "提示");
                            return;
                        }
                    }
                }
                MessageBox.Show("删除失败!", "提示");
                return;
            }
            else//取消
            {
                return;
            }
        }
Esempio n. 6
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 成果类型组合框更新
            if (MyDatabase.TestMyDatabaseConnect() == false)                                          //数据库连接失败
            {
                MessageBox.Show("数据库连接失败!", "提示");
                return;
            }
            try
            {
                this.cboAchievementType.Items.Clear();      //清空combox内所有选项
                DataTable dt_1 = MyDatabase.GetDataSetBySql("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.GetDataSetBySql(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
        }