Hi, I have a question a bout a data bound data grid.

The grid has 5 columns, the first of which is read only. I thought that setting the column to read only would prevent the user from being able to alter the cell value.

However, it apears that I was wrong, not the first time definatly not the last :>.

Now the program throughs a read only error which i need to handly. Can any one please tell me which event is trigering the error.

The program still runs but i need to change the error displayed to the user and override the current one.

Thanks for reading

Dani AI

Generated

raised a common case: a bound DataGridView with five columns where the first column was intended to be readonly, but the UI still allowed input and the app later threw a read-only error. pointed to using the debugger/stack trace and hinted at the grid's error path. Below are practical checks and handlers to diagnose and give a friendly message instead of the default exception.

First checks (no guesswork)

  • Confirm the column's ReadOnly is actually set after the grid is bound (AutoGenerateColumns can recreate columns). Example: set Columns["YourColumnName"].ReadOnly = true after assigning DataSource.
  • Check the underlying data source: a DataTable column or the bound object's property can be read-only and cause the write to fail when the grid tries to commit.
  • Verify you are not programmatically writing into that cell elsewhere.

Handle the grid error flow (inspect and control)

Example handlers

  • Cancel attempts to begin editing the readonly column (prevents the error bubbling up):
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    if (e.ColumnIndex == 0) { e.Cancel = true; /* show custom text or tooltip */ }
}
  • Catch commit errors and show a friendly message while stopping a rethrow:
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    // log e.Exception
    MessageBox.Show("That column cannot be changed.");
    e.ThrowException = false;
}

Caveat: do not silently swallow exceptions—log them so you can fix the underlying cause. If you need the exact throwing point, enable "Break on thrown exceptions" in Visual Studio and inspect the stack trace as suggested.

Recommended Answers

All 2 Replies

In the debugger look in the stack trace.
You will find there the last thing that happened before your program crashed.

right click the datagridview select properties and click events tab, there there is a dataerror event, you can handle that error to generate custom error message. there must be some other error events too, but i am in rush for the time being.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.