Question

I have a datagridview where all the columns are bound except for one checkbox column. In my form's OnLoad I go through the rows in my dataGridView and set DataGridViewCheckBoxCell.Value = true for each row.

I've verified at the end of my OnLoad that all the DataGridViewCheckBoxCells that I set still have the values I gave them, but once the dataGridView is displayed all the selected values and checkbox values I set get reset.

Was it helpful?

Solution

Apparently , when a DataGridView becomes visible (Visible = true), OnBindingContextChanged gets called causing the internal data connection to be reset and reset the values on all the cells.

So instead of setting the values of cell in my form's OnLoad I set VirtualMode = true on my data grid and override OnCellValueNeeded where I can provide the check box cell's value on demand.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MyControls
{
public class SelectedItemsGridView : DataGridView
{
    private IList _SelectedItems;
    public IList SelectedItems 
    {
        get { return _SelectedItems; }
        set
        {
            _SelectedItems = value;
            ClearSelection();
            Refresh();
        }
    }

    public SelectedItemsGridView()
        : base()
    {
        SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        RowHeadersVisible = false;
        VirtualMode = true;
        ////Columns.Add(new DataGridViewCheckBoxColumn(false) { 
        //    AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
        //    HeaderText = "Select"});
    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        if (DesignMode == true) { return; }
        Columns.Insert(0, new DataGridViewCheckBoxColumn(false)
        {
            AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader,
            HeaderText = "Select"
        });
    }

    public bool IsItemSelected(object obj)
    {
        if (SelectedItems == null) { return false; }
        return SelectedItems.Contains(obj);
    }

    protected override void OnCellValueNeeded(DataGridViewCellValueEventArgs e)
    {
        base.OnCellValueNeeded(e);
        if (e.ColumnIndex == 0)
        {
            e.Value = IsItemSelected((this.DataSource as IList)[e.RowIndex]);
        }
    }

    protected override void OnCellContentClick(DataGridViewCellEventArgs e)
    {
        base.OnCellContentClick(e);
        if (e.RowIndex == -1) { return; }
        Object item = ((IList)DataSource)[e.RowIndex];
        if(e.ColumnIndex == 0)
        {
            var cellValue = this[e.ColumnIndex, e.RowIndex].Value;
            if (cellValue != null && (bool)cellValue == true)
            {
                SelectedItems.Remove(item);

            }
            else if (cellValue != null && (bool)cellValue == false)
            {
                SelectedItems.Add(item);
            }
        }
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top