The activist is not the man who says the river is dirty. The activist is the man who cleans up the river.
H. Ross Perot

You must not lose faith in humanity. Humanity is an ocean; if a few drops of the ocean are dirty, the ocean does not become dirty.
Mahatma Gandhi

I can remember when the air was clean and sex was dirty.
George Burns

Sometimes my windows applications have a check box column or combo box. By clicking the check box or changing the combo box selected value, the desire is for the under lying data to be changed immediately. This is NOT what happens.

By adding a few lines of code to the DataGridView CurrentCellDirtyStateChanged event, the desired functionality happens.

Start with a simple data class.

public class Employee
{
  public string EmployeeId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public bool IsActive { get; set; }

  public Employee()
  {
    EmployeeId = string.Empty;
    FirstName = string.Empty;
    LastName = string.Empty;
  }
}

Populate an employee list. Bind the list to the data grid.

private void Main_Load(object sender, EventArgs e)
{
  colActive.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  List<Employee> employeeList = new List<Employee>();
  employeeList.Add(new Employee() { EmployeeId = "12345", LastName = "Jones", FirstName = "Susan", IsActive = true });
  employeeList.Add(new Employee() { EmployeeId = "12233", LastName = "Jackson", FirstName = "Carl", IsActive = true });
  employeeList.Add(new Employee() { EmployeeId = "34455", LastName = "Edwards", FirstName = "Thomas", IsActive = false });
  employeeList.Add(new Employee() { EmployeeId = "23231", LastName = "Stevens", FirstName = "Henry", IsActive = true });
  employeeList.Add(new Employee() { EmployeeId = "76112", LastName = "Smith", FirstName = "Jacob", IsActive = true });
  employeeList.Add(new Employee() { EmployeeId = "10120", LastName = "Davis", FirstName = "Mary", IsActive = false });
  employeeList.Add(new Employee() { EmployeeId = "10299", LastName = "Anderson", FirstName = "Helen", IsActive = true });
  bsDirtyCell.DataSource = employeeList;
}

The grid should look something like the following. After wiring the CurrentCellDirtyStateChanged event of the DataGridView, now when the check box is clicked, the under lying data object is changed immediately.

The code below checks to see if the column is the check box column. As you can see, I am in the habit of checking the CurrentCell for null. Many times it is null in events when I would never expect it to be null. I just got in the habit of always checking.

private void dgvDirtyCell_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
  if (dgvDirtyCell.IsCurrentCellDirty)
  {
    if (dgvDirtyCell.CurrentCell != null)
    {
      if (dgvDirtyCell.Columns[dgvDirtyCell.CurrentCell.ColumnIndex] == colActive)
        dgvDirtyCell.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
  }
}

Download Solution

Tags: ,

Leave a Reply


*