views:

149

answers:

1

I'm trying to override a DataGridViewCheckBox so that it takes it's boolean value from whether an object is in a collection (essentially a predicate), and when the value is set it adds/removes the object from the collection as appropriate.

Furthermore, I want this value to be checked when the checkbox is displayed (but I can't set Value until after the DataGridView is assigned). I've tried various combinations of method overrides on the CheckBoxCell (GetValue/SetValue don't seem to work), and any solution I try seems unneccessarily complicated.

Whats the best, most sensible and least hacky way to override the checkboxcell value in this way?

+1  A: 

I guess you can create a custom MyDataGridViewCheckBoxCell and override its GetFormattedValue to return true\false depending on the existence cell's value in your collection; and SetValue to modify the collection. Please see if an example below would work fine for you; not sure if it's the best way to do this but it's not hacky that's for sure :)

private static List<string> ItemsList = new List<string>();
...
// fill in collection list
ItemsList.AddRange(new string[] { "a", "c" });
// create columns
DataGridViewTextBoxColumn column0 = new DataGridViewTextBoxColumn() 
    {  HeaderText = "column1", DataPropertyName = "Column1"};
DataGridViewCheckBoxColumn column1 = new NewDataGridViewCheckBoxColumn()
    {  HeaderText = "column2", DataPropertyName = "Column2"};
dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { column0, column1 });
// create data rows
dataSet1.Tables[0].Rows.Add(new object[] {"a", "a" });
dataSet1.Tables[0].Rows.Add(new object[] { "b", "b" });
dataSet1.Tables[0].Rows.Add(new object[] { "c", "c" });
...
// custom datagridview checkbox cell
public class MyDataGridViewCheckBoxCell : DataGridViewCheckBoxCell 
{
    public MyDataGridViewCheckBoxCell()
    {
        FalseValue = false;
        TrueValue = true;
    }

    protected override Object GetFormattedValue(Object value,
        int rowIndex,
        ref DataGridViewCellStyle cellStyle,
        TypeConverter valueTypeConverter,
        TypeConverter formattedValueTypeConverter,
        DataGridViewDataErrorContexts context)
    {
        // check if value is string and it's in the list; return true if it is
        object result = (value is string) ? Form1.ItemsList.IndexOf((string)value) > -1 : value;
        return base.GetFormattedValue(result, rowIndex, ref cellStyle, 
            valueTypeConverter, formattedValueTypeConverter, context);
    }

    protected override bool SetValue(int rowIndex, Object value)
    {
        if (value!=null)
        {
            // change collection
            if (value.Equals(true))
                Form1.ItemsList.Add((string)Value);
            else
                Form1.ItemsList.Remove((string)Value);

            // dump list into console
            foreach (string item in Form1.ItemsList)
                Console.Write("{0}\t", item);
            Console.WriteLine();
        }
        return true;
    }
}        
// custom datagridview column     
public class NewDataGridViewCheckBoxColumn : DataGridViewCheckBoxColumn
{
    public NewDataGridViewCheckBoxColumn()
    {
        CellTemplate = new MyDataGridViewCheckBoxCell();
    }
}

hope this helps, regards

serge_gubenko