Wednesday, June 20, 2012

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


In this post we shall see on how to get the rows which are added newly to 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 are added newly 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 newly inserted rows and inserts the details into the database by forming INSERT 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 Newly added Rows by filtering on their RowState
    dtChanges = dtEmployee.GetChanges(DataRowState.Added);
    if (dtChanges != null)
    {               
        // Form the INSERT Query to insert the New Rows.
        objConn.Open();
        objCmd.Connection = objConn;
        for (int i = 0; i < dtChanges.Rows.Count; i++)
        {
            strQuery = "INSERT INTO Employee (Name,DOB,DOJ,Phone,Email,DepartmentID,Salery)";
            strQuery += " VALUES (";
            strQuery += "'" + dtChanges.Rows[i]["Name"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["DOB"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["DOJ"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["Phone"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["Email"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["DepartmentID"].ToString() + "',";
            strQuery += "'" + dtChanges.Rows[i]["Salery"].ToString() + "')";
            //
            // Execute the Insert Query.
            objCmd.CommandText = strQuery;
            objCmd.ExecuteNonQuery();
        }
        objConn.Close();
        dtChanges = null;
    }
}

The DataTable.GetChanges(DataRowState.Added), returns a DataTable which contains the detials of all the rows which are added newly to the DataGridView.

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

Search Flipkart Products:
Flipkart.com

No comments: