DataViews are typically used in C# to represent a set of data obtained from the sources like a database or a dataTable from a different perspective. DataView often exposes several methods in sorting, searching and manipulating the data. In this example, we modify a row in an existing DataView with a new value. The dynamic nature of DataView makes it different from the other methods provided by the DataTable class. When there are joined tables, use of DataView is not recommended as it can get vulnerable in the complex scenarios.

Edit DataView Row in C#

In our example, we override the constructor of the DataView class to take four parameters.

  1. DataTable name
  2. RowFilter(String)
  3. Sorting column(String)
  4. DataViewRowState
DataSet dataSet = new DataSet();
DataView dataView;

try {
    //open the database connection
    connection.Open();

    //set the query to command
    command = new SqlCommand("SELECT * FROM Employee", connection);
    sqlDataAdapter.SelectCommand = command;

    //set the table to dataset
    sqlDataAdapter.Fill(dataSet, "Employee");

    //free unmanaged resources
    sqlDataAdapter.Dispose();
    command.Dispose();

    //close the connection
    connection.Close();

    //override DataView Constructor
    dataView = new DataView(dataSet.Tables[0], "", 
                            "id", DataViewRowState.CurrentRows);

    //find the row index of a particular employee
    int rowIndex = dataView.Find("CA001");

    //when employee is not found in the database
    if (rowIndex == -1) {
         Console.WriteLine("Employee unavailable");
    }
    
    //update the ID of employee row
    else {
         dataView[rowIndex]["id"] = "BA001";
         Console.WriteLine("Employee Updated Successfully");
    }

 }
 catch (Exception ex) {
    Console.WriteLine(ex);
 }

Code Explanation

After creating the database connection, set the query and send the command to the database using SqlCommand() method. In here, we are going to update an employee whose ID is “CA001” to “BA001”. Therefore, after querying the employee table, set it to the dataset. It is important to dispose of the sqlAdapter and command instances to avoid unnecessary memory allocation after the execution. As we have discussed earlier, we have overridden the DataView constructor with four parameters. In this article, we only focus on the row updating. Therefore we set an empty string for the filterRow object. It is important to set the correct column name with the correct letters to the sorting column as it is case-sensitive. We set the last property to DataViewRowState.CurrentRows which is the default option of it.

Then we need to find whether the row we want to update is available or not. For that DataView provides a method called find() which takes one parameter and searches the required row from the DataView by matching with the value of the column we specified in the DataView constructor. If the result is found, the specified value is set to that particular column and printed out the response message to the user.

Output:

Dataview Updated

Recommended Reading

Last modified: March 20, 2019