3

I want to add checkbox in a specific row and column but i'm always stumbled upon this error

"System.FormatException: Formatted value of the cell has a wrong type."

And here is my code to add the checkbox;

    private void checkboxSource(string columnSource, int n)
    {
        DataGridViewCheckBoxCell checkboxColumn = new DataGridViewCheckBoxCell();
        checkboxColumn.FalseValue = "0";
        checkboxColumn.TrueValue = "1";
        dataGridView1.Rows[n].Cells[6] = checkboxColumn;
    }

I know something is wrong when i try to bind checkboxColumn to datagridview. Can someone please guide me on how to bind the checkbox to datagridview properly provided which row and cell are taken into account. Thank you in advance.

1 Answer 1

1

The error is because the cell contains null value. You should set Style.NullValue of the cell to false. The property sets the display value when cell value is DBNull.Value or null:

var cell = new DataGridViewCheckBoxCell()
{
    TrueValue = "1",
    FalseValue = "0",
};
cell.Style.NullValue = false;
this.dataGridView1.Rows[2].Cells[0] = cell;
Sign up to request clarification or add additional context in comments.

1 Comment

Usually you should create a column of type DataGridViewCheckBoxColumn instead of changing the cell type. But if for some reason you need to change a specific cell type, you can change the cell type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.