Delete data (true and false deletion method) in DataGridView and update to sql database

Delete the data in the DataGridView control on the winform end of the host computer and update it to the database

First introduce the two deletion methods, true deletion and false deletion, first introduce false deletion, followed by true deletion
true deletion:
true deletion refers to the actual deletion of the data in the database, will not be displayed again, and then need to The data should be added again. The delete statement is used for
false deletion : it is
not really deleting the data in the database, but just setting a flag bit IsDeleted in the data column that you want to delete, assuming that the flag bit is 0 by default, think When deleting a row of data, set the flag to 1, and use the update statement
so that the data is still in the database, but it is no longer displayed in the host computer DataGridView. The specific process is as follows:

  • This is the data information
    in the database. When designing the table, add a column of IsDeleted of type int and set it to 0 by default.
    Insert picture description here
    Insert picture description here
  • When the host computer loads the information in the DataGridView control, the SQL statement used is as follows:
    pay attention to add a sentence where s.IsDeleted = 0 This will show that the default IsDeleted = 0 is all the data rows The
    following figure is the SQL statement in the host computer As well as the execution of query statements in SQL, the displayed query results
    Insert picture description here
    Insert picture description here
  • This is the display information of the host computer interface.
    How to display the first column and the last two columns is to add the Link column and Checkbox column in the datagridview. For details, refer to the previous blog post
    https://blog.csdn.net/qq_39217004/article/details/ 105364814

Insert picture description here

The following describes how to implement in the code, the steps and ideas of fake deletion

  1. Set the CellContentClick registration event of the control datagridview, and click the content in the cell to trigger the event
  2. Add code in the CellContentClick event
  3. First, get the row and column index of the currently clicked cell (the rows and columns to determine the selected data)
  4. If you click the delete key in the last column of the nth row, the method is executed
  5. First obtain the row data clicked by the mouse, and use datagridview to turn to datarow
  6. Write sql statement to set the flag bit of the selected row data to 1 in the database
  7. Refresh in the datagridview, remove the datarow just selected

Select an object to be deleted, click delete, and then click OK, it prompts that the deletion is successful, and the information on the interface also disappears.
Insert picture description here
Insert picture description hereInsert picture description here
Open the database to view the results,
click execute, you can see that the isDeleted flag of the selected row on the interface has changed
9ibG9nLmNzZG4ubmV0L3FxXzM5MjE3MDA0,size_16,color_FFFFFF,t_70)
Start the program page again for 1 and you will find that the deleted "Wang Feng" information is no longer displayed on the page
Insert picture description here

private void dgvStudents_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex != -1)  //鼠标点击的位置在  列标题位置下的数据,才触发
    {
        //1 获取我当前点击的单元格的行列数索引
        //2 判断是否选择的是link列,并且判断是 删除还是修改
        DataGridViewCell cell = dgvStudents.Rows[e.RowIndex].Cells[e.ColumnIndex];//1      
        if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "删除")
        {
            //删除操作
            DialogResult drResult = MessageBox.Show("你确定要删除该学生的信息吗?", "删除学生信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (drResult == DialogResult.Yes)
            {
                //获取行数据
                //dgvStudents.Rows[e.RowIndex]是数据表中鼠标点击的行索引   DataRow表示表中的一行数据
                //(xxx).Row才获取到了所选中的行列索引所绑定的对象
                //DataGridViewRow转换为DataRow
                DataRow dr = (dgvStudents.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                int stuId = int.Parse(dr["stuId"].ToString());//将stuId字符串型转化为整型
                                                              //假删除 IsDeleted
                string sqlDel0 = "update StudentInfo set IsDeleted=1 where StuId=@stuId";
                SqlParameter para = new SqlParameter("@StuId", stuId);
                int count = SqlHelper.ExecuteNonQuery(sqlDel0, para);
                if (count > 0)
                {
                    MessageBox.Show("该学生信息删除成功", "删除学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //DataGridView中的数据并没有刷新,需要手动刷新一下
                    DataTable dtStudents = (DataTable)dgvStudents.DataSource;//获取到数据源
                    dtStudents.Rows.Remove(dr);//将获取到的行列索引所绑定的对象移出
                    dgvStudents.DataSource = dtStudents;//重新指定数据源
                }
                else
                {
                    MessageBox.Show("该学生信息删除失败", "删除学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }         
          }
        }
    }

The following introduces true deletion :
on the basis of false deletion, you only need to change the sql statement slightly. Change
1:
string sqlDel0 = "delete StudentInfo where StuId = @ stuId";
Change 2: When
loading datagridview, change the sql statement to the
Insert picture description here
following view Debugging result:
select a deleted data in the control, click: delete, the information on the interface disappears.
It was found in sql that the information of "Wang Feng" in the student table was also completely deleted, achieving the desired effect
Insert picture description here
Insert picture description here

At this point, the function has been completed

Published 18 original articles · praised 0 · visits 233

Guess you like

Origin blog.csdn.net/qq_39217004/article/details/105366769