Wednesday, June 20, 2012

Get Deleted rows in a DataTable / DataGridView in C# Windows Forms


In this post we shall see on how to get the rows which are Deleted in a DataGridView in a C# Windows forms application.

Unlike web applications, the Win Apps allow users to perform multiple operations like INSERT, MODIFY, DELETE to the data rows which are loaded in the DataGridView, once done the user clicks the Save button to send all the updates to the database at one shot.

Here we will see on how to capture the details of the rows which were deleted by the user.
Initially load the DataGridView with data and allow the user to work on the data.

private void btnLoadEmployees_Click(object sender, EventArgs e)
{
    // Get the Connection string from App.config.
    string strConn = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    //
    // Query the DB to get the Data           
    SqlConnection objConn = new SqlConnection(strConn);
    SqlCommand objCmd = new SqlCommand("SELECT * FROM EMPLOYEE", objConn);
    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
    DataSet dsEmployee = new DataSet();
    objDA.Fill(dsEmployee, "dtEmployee");
    //
    // Populate the GridView with Data
    gvEmployees.DataSource = dsEmployee.Tables["dtEmployee"];
}

Once the user is down with all the chages, he will click on the Save button, the following code filters the deleted rows and deleted the details from database by forming DELETE queries.

private void btnSave_Click(object sender, EventArgs e)
{
    string strQuery = string.Empty;
    DataTable dtChanges;
    //
    // Get the Updated DataTable back from the DataGridView
    DataTable dtEmployee = (DataTable)gvEmployees.DataSource;
    //
    // Get the Connection string from App.config.
    string strConn = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    SqlConnection objConn = new SqlConnection(strConn);
    SqlCommand objCmd = new SqlCommand();
    //
    // Get the Deleted Rows by filtering on their RowState
    dtChanges = dtEmployee.GetChanges(DataRowState.Deleted);
    if (dtChanges != null)
    {
        //
        // Form the DELETE Query to Delete the Rows.
        objConn.Open();
        objCmd.Connection = objConn;
        for (int i = 0; i < dtChanges.Rows.Count; i++)
        {
            strQuery = "DELETE FROM Employee ";
            strQuery += "WHERE ID = '" + dtChanges.Rows[i]["ID", DataRowVersion.Original].ToString() + "'";
            //
            // Execute the Insert Query.
            objCmd.CommandText = strQuery;
            objCmd.ExecuteNonQuery();
        }
        objConn.Close();
        dtChanges = null;
    }
}

The DataTable.GetChanges(DataRowState.Deleted), returns a DataTable which contains the detials of all the rows which were deleted in the DataGridView.

Notice that we are using DataRowVersion.Original, along with the column name ID, this is because the row is deleted and the values from the deleted row cannot be obtained.

If we use dtChanges.Rows[i]["ID"], instead of dtChanges.Rows[i]["ID", DataRowVersion.Original], then the following exception will be thrown.

System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.

To see on how to get the newly added rows refer the post Get new rows in a DataTable / DataGridView in C# Windows Forms
To see on how to get the Modified rows refer the post Get Modified rows in a DataTable / DataGridView in C# Windows Forms.


Search Flipkart Products:
Flipkart.com

No comments: