New to Telerik UI for WinFormsStart a free 30-day trial

Handle Add, Delete and Update of Rows

Updated over 6 months ago

When the user adds new rows, updates or deletes the existing ones, the external data source should be updated as well. Follow the steps below in order to keep the data synced:

1. Handle the CellValuePushed event in order to detect when a cell value is changed.

Push value to the data source

C#
    
private void radVirtualGrid1_CellValuePushed(object sender, VirtualGridCellValuePushedEventArgs e)
{
    this.UpdateCellValue(data[e.RowIndex].CustomerId, columnNames[e.ColumnIndex], Convert.ToString(e.Value));
}
    
private void UpdateCellValue(string id, string columnName, string value)
{
    using (System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(@"UPDATE Customers SET " + columnName + " = ? WHERE CustomerID = ?"))
    {
        command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@columnValue", value));
        command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@customerId", id));
        command.Connection = new System.Data.OleDb.OleDbConnection(connectionString);
        command.Connection.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
    }
    SelectData();
}

2. When the user adds a new row in the grid, it is necessary to subscribe to the UserAddedRow event in order to update the data source:

Add new row

C#
        
private void radVirtualGrid1_UserAddedRow(object sender, VirtualGridNewRowEventArgs e)
{
    List<object> newValues = new List<object>();
    for (int i = 0; i < columnNames.Length; i++)
    {
        newValues.Add(e.NewValues[i]);
    }
    this.AddDataRow(newValues);
}
        
private void AddDataRow(List<object> newValues)
{
    using (OleDbCommand command = new OleDbCommand(@"INSERT INTO Customers (CustomerID, CompanyName," +
                                                   " ContactName, ContactTitle, Address, PostalCode)  values (?, ?, ?, ?, ?, ?)"))
    {
        command.Parameters.Add(new OleDbParameter("@param1", GenerateID()));
        command.Parameters.Add(new OleDbParameter("@param2", newValues[0]));
        command.Parameters.Add(new OleDbParameter("@param3", newValues[1]));
        command.Parameters.Add(new OleDbParameter("@param4", newValues[2]));
        command.Parameters.Add(new OleDbParameter("@param5", newValues[3]));
        command.Parameters.Add(new OleDbParameter("@param6", newValues[4]));
        command.Connection = new OleDbConnection(connectionString);
        command.Connection.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
    }
    int currentColumn = this.radVirtualGrid1.VirtualGridElement.CurrentCell.ColumnIndex;
    SelectData();
    this.radVirtualGrid1.VirtualGridElement.InputBehavior.SelectCell(data.Count - 1, currentColumn, false,
        false, this.radVirtualGrid1.VirtualGridElement.MasterViewInfo);
}
        
private string GenerateID()
{
    StringBuilder sb = new StringBuilder();
    Random rand = new Random();
    for (int i = 0; i < 5; i++)
    {
        sb.Append((char)('A' + rand.Next(26)));
    }
    return sb.ToString();
}

3. When the user deletes an existing row, you should handle the UserDeletingRow event to update the data source as well:

Delete row

C#
        
private void radVirtualGrid1_UserDeletedRow(object sender, VirtualGridRowsEventArgs e)
{
    string query = "";
    foreach (var item in e.RowIndices)
    {
        query += "'" + data[item].CustomerId + "',";
    }
    DeleteDataRow(query.TrimEnd(','));
}
private void DeleteDataRow(string query)
{
    using (OleDbCommand command = new OleDbCommand("DELETE FROM Customers where CustomerID In  (" + query + ")"))
    {
        command.Connection = new OleDbConnection(connectionString);
        command.Connection.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
    }
    SelectData();
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support