Set the checkbox of a DataGridViewCheckBoxCell to true


I load a DataGridView through a list.

datagridview.DataSource = listaVariables;

This DatagridView, is formed by a column (the first) that is of the DataGridViewCheckBoxCell type.

What I want to do is that, depending on a property of the variables in the list, it is activated as marked or not.

I've tried to do this:

datagridview.Rows[pos].Cell[0].Value = true; //pos es un entero que utilizo para recorrer las filas y pongo 0 en Cell porque es la primera columna la que tiene los CheckBox

and does nothing, leaves it unmarked.

I have also tried to create a cell of the DataGridViewCheckBoxCell type, starting it true but it does not work either, it gives me an error of formatting the cell (System.FormatException: The formatted value of the cell has an erroneous type):

DataGridViewCheckBoxCell dC = new DataGridViewCheckBoxCell(true);
datagridview.Rows[pos].Cell[0] = dC;

In this last test I think that the cell that I believe (although I do not allow it to be assigned) that I would not put the checkbox that contains true.

I've also tried to do:

dataGridView.Rows[pos].SetValues(true); //Se supone que la primera celda de la fila la debería poner a true y no lo hace


var values = new bool[] { true };

and I still have the same result, it does not show me the activated checkbox.

The list with which I charge the datagrid I define it like this:

List<PlacaVariableP> listaVariables;

The Variable object is as follows:

public class PlacaVariableP:IComparable
    public virtual long id_placavariable { get; set; }
    public virtual long idplaca { get; set; }
    public virtual string descripcion { get; set; }
    public virtual string unidades { get; set; }
    public virtual string idFichero { get; set; }

    public virtual int CompareTo(object obj)
        PlacaVariableP c = (PlacaVariableP)obj;
        return String.Compare(this.descripcion, c.descripcion, StringComparison.Ordinal);

    public virtual void toString(object obj)
asked by Pablo Simon DiEstefano 13.09.2017 в 14:01

2 answers


The problem is that when working with a DataGridView you have two ways of doing it:

  • Using DataSource:
  • In this way you set the content to be displayed in the DataGridView through the DataSource property and this is the information that is displayed.

    If you want to make changes you must make them in the object that you have established as DataSource and re-associate it. You can not modify the contents of the cells directly.

  • Manipulating rows and cells:
  • You can fill in the content by adding DataGridViewRow objects to the Rows collection of the DataGridView and setting the values of each cell.

    This way you can manipulate the contents of the cells directly.

    Therefore, if you are going to use DataSource, the elements contained in the list that you associate with the DataSource should have a property to set the value of the check type column.

    You could do something like this:

            dataGridView1.DataSource = listaVariables.Select(x =>
                    columnaCheck = x.id_placavariable > 20,

    In this way the elements of the DataSource will have a property (in the example columnaCheck ) with the value for the column.

    answered by 14.09.2017 / 09:18

    You could use the DataGridViewCheckBoxColumn. A simple example would be:

    bool[] arr = new bool[3];
            arr[0] = true;
            arr[1] = false;
            arr[2] = true;
            DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
            col1.FalseValue = false;
            col1.TrueValue = true;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell) dataGridView1.Rows[i].Cells[0];
                if (arr[i] == true)
                    check.Value = check.TrueValue;
    answered by 13.09.2017 в 15:43