JohnWayne
JohnWayne

Reputation: 115

DataGridView.DataError.ReadOnlyException

I get this error when I want to apply changes to the datagridview's data. The interesting thing is that the error only occur after midnight (00:00) before that it works.

if (dgvDatabaseData.CurrentCell.ColumnIndex == 11) {
    if (dgvDatabaseData.CurrentCell.Value == null ||
        dgvDatabaseData.CurrentCell.Value == DBNull.Value ||
        String.IsNullOrWhiteSpace(dgvDatabaseData.CurrentCell.Value.ToString()))
    {

        dgvDatabaseData.CurrentCell.Value = "Start Up";         // (Error occurs here)
        dgvDatabaseData.CurrentRow.Cells[10].Value = "On Hold"; // (Error occurs here)
    }
    else {
        dgvDatabaseData.CurrentCell.Value += " + Start Up";
    }
    dgvDatabaseData.NotifyCurrentCellDirty(true);
}

This is the error that gets displayed

The DGV's ReadOnly is set to false

Upvotes: 0

Views: 417

Answers (1)

JohnG
JohnG

Reputation: 9479

I am somewhat amused as to why you would think that the code would quit working because it is after midnight? Gremlins coding after midnight? :-) I can assure you that the time is NOT the problem.

From the error, it appears clear that the DataSource which I assume is a DataTable has column 11 set to read only. The DataTables column read only property, will override the grids read only property when set to true. Therefore, if the grids DataSource IS a DataTable, then simply set that columns read only property to false before you try to set the cells value.

If the grids DataTable is global, then simply…

myDataTable.Columns[11].ReadOnly = false;

If the grids DataSource is not global, yet it is a DataTable, then…

DataTable dt = (DataTable)dataGridView1.DataSource;
dt.Columns[11].ReadOnly = false;

Lastly, I would assume that this column is being set to read only elsewhere, or it is coming from the DB as a read only. It is unclear if the column should be read only or not. If it is intended to be read only, then you may want to turn the columns read only property back to true after the code has finished setting the cells values.

Upvotes: 3

Related Questions