C# WinForm 实现增删改查等功能(Access版) 系列之三

C# WinForm 实现增删改查等功能(Access) 系列之三

更新一条记录模块设计与实现

设计:点击更新按钮弹出一个窗体(在这里是对dataGridView的一个双击事件),在窗体的文本框中填充数据,点击更新按钮,当前修改数据窗体自动关闭,同时刷新主窗体,更新并显示数据。

实现:需要在增加按钮事件中传递一个字符串和一个主窗体的指针。

/// <summary>

        /// 修改

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            dataGridView1_DoubleClick(sender, e);//类似于dataGridView的更新操作,也就是双击操作

        }

 

//右键选中行

//本方法来自CSDN博客,转载请标明出处:http://blog.csdn.net/yshjshxxn/archive/2009/06/26/4300927.aspx

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

        {

              //判断如果点击的是鼠标右键  

            if (e.Button == MouseButtons.Right)  

            {  

                //判断鼠标点击在数据行上  

                if (e.RowIndex >= 0)  

                {

                    dataGridView1.ClearSelection();

                    dataGridView1.Rows[e.RowIndex].Selected = true;

                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];  

                }  

            }

        }

 

/// <summary>

        /// dataGridview1双击事件,获得某行的信息

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void dataGridView1_DoubleClick(object sender, EventArgs e)

        {

            //selectedRowsButton_Click(sender, e);

            Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);

            if (selectedRowCount > 0)

            {

                String str = string.Empty;

                for (int i = 0; i < selectedRowCount; i++)

                {

                    int selectedRow = dataGridView1.SelectedRows[i].Index;    //获得选中的某行

                    int columns = dataGridView1.ColumnCount;

                    for (int j = 0; j < columns; j++)

                    {

                        str += dataGridView1.Rows[selectedRow].Cells[j].Value.ToString() + ",";

                    }

                }

                ResumeAddOrUpdate resumeAdd = new ResumeAddOrUpdate(str,this);

                resumeAdd.SaveOrUpdate("Update");

                resumeAdd.Width = this.Width;   //设置宽度,高度

                resumeAdd.Height = this.Height;

                resumeAdd.Top = this.Top;

                resumeAdd.Left = this.Left;

                resumeAdd.StartPosition = FormStartPosition.CenterScreen;

                resumeAdd.BackColor = Color.White;

                resumeAdd.Show();

              //  MessageBox.Show(str.ToString(), "Selected Rows");

            }

        }

 

 

ResumeAddOrUpdate窗体实现:

/// <summary>

        /// 增加Or更新

        /// </summary>

        /// <param name="str"></param>

        public void SaveOrUpdate(string str)

        {

            if (str.ToLower().Equals("save"))

            {

                btnSave.Visible = true;

                btnUpdate.Visible = false;

            }else if(str.ToLower().Equals("update")){

                btnSave.Visible = false;

                btnUpdate.Visible = true;

                txtName.Enabled = false;

            }

        }

 

/// <summary>

        /// 更新时的操作

        /// 两个参数的构造函数

        /// 传递一个父窗体

        /// </summary>

        /// <param name="str"></param>

        /// <param name="_resumeTest"></param>

        public ResumeAddOrUpdate(string str,ResumeTest _resumeTest)   //这里需要从父窗体中传递一个this指针

        {

            InitializeComponent();

            this.resumeTest = _resumeTest;

            timeStamp = 0;//初始化参数

            lblImage.Text = "0";//初始化是否更新图片,为空时不更新图片,不为空时则更新图片

            string[] content = str.Split(',');

            txtName.Text = content[0];   //姓名

            if (content[1].Equals(""))

            {

                rbtnMale.Checked = true;

            }

            else if (content[1].Equals(""))

            {

                rbtnFemale.Checked = true;

            }

            dateTimePicker1.Text = content[2].ToString();

            txtYear.Text = content[3].ToString();

            txtCardStyle.Text = content[4].ToString();

            txtCardNumber.Text = content[5].ToString();

            txtAddress.Text = content[6].ToString();

            txtEmail.Text = content[7].ToString();

            txtPhoneNumber.Text = content[8].ToString();

            txtHomeNumber.Text = content[9].ToString();

            txtSelfCommand.Text = content[10].ToString();

            string strPictrueBox = getStrOfPictureBox(txtName.Text.Trim());  //根据数据库中生成的图片路径,显示图片

            //MessageBox.Show(strPictrueBox, "提示");

          

            if (strPictrueBox.Contains("nophoto.jpg"))

            {

                lblImage.Text = "0";   //给图片路径字符串赋值

                picPerson.Image = Image.FromFile(Application.StartupPath + "//Image//nophoto.jpg");

            }

            else

            {

                lblImage.Text = strPictrueBox;   //给图片路径字符串赋值

               

                picPerson.Image = Image.FromFile(Application.StartupPath + "//Image//" + strPictrueBox + ".jpg");

            }

        }

 

 

/// <summary>

        /// 修改操作

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnUpdate_Click(object sender, EventArgs e)

        {

//手动代码保存数据至数据库

            OleDbConnection conn = GetConnection();

            string sqlText = "Update MResume set 性别=@sex,出生日期=@datetimes,工作年限=@years,证件类型=@cardStyle,证件号=@cardNumber,居住地=@address,Email=@email,手机号码=@phoneNumber,家庭电话=@homeNumber,自我评价=@selftCommand,图片=@timeStamp where 姓名=@name";

          //  OleDbTransaction transaction = null;

            OleDbCommand cmd = new OleDbCommand(sqlText, conn);

           // transaction = conn.BeginTransaction();

           // cmd.Transaction = transaction;

            cmd.Parameters.AddWithValue("@sex", sex);

            cmd.Parameters.AddWithValue("@datetimes", datetimes);

            cmd.Parameters.AddWithValue("@years", years);

            cmd.Parameters.AddWithValue("@cardStyle", cardStyle);

            cmd.Parameters.AddWithValue("@cardNumber", cardNumber);

            cmd.Parameters.AddWithValue("@address", address);

            cmd.Parameters.AddWithValue("@email", email);

            cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);

            cmd.Parameters.AddWithValue("@homeNumber", homeNumber);

            cmd.Parameters.AddWithValue("@selftCommand", selftCommand);

            cmd.Parameters.AddWithValue("@timeStamp", lblImage.Text);

            cmd.Parameters.AddWithValue("@name", name);

            try

            {

                conn.Open();

                int i = cmd.ExecuteNonQuery();

                MessageBox.Show("更新成功!", "提示");

                this.resumeTest.RefreshDataGridView(sender,e);   //刷新窗体

                this.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show("异常:" + ex.ToString(),"提示");

            }

            finally

            {

                conn.Close();

            }

        }

 

你可能感兴趣的:(String,object,C#,Access,email,WinForm)