Editing DataGridView Cell Always Reverts

R

rob

I have a BindingSource whose DataSource property is set to a list of
structs (List<MyStruct>). Each property in MyStruct can be read and
writen (get/set).

I also have a DataGridView to which I manually add columns
(dataGridView.Columns.Add). Each column has set the property "ReadOnly"
to false. The DataPropertyName is one of the properties of MyStruct.
ValueType is set to the type that property, i.e. String, Boolean,
etc.Finally the DataSource of dataGridView is set to the above
BindingSource.

All of this works well. I am also able to edit the cells. But as soon
as I exit the cell, i.e. move to another sell, the changes are reverted
to the original value. I suspect that something goes wrong with an
attempted (automatic) synchronization between the datasource, i.e.
BindingSource, and the new value in the cell. I can't figure out what
is going on, though. Any help is appreciated.

Thanks
 
D

Dmytro Lapshyn [MVP]

Hi Rob,

Your assumption is most likely correct. What you can try is configuring the
IDE's debugger to break immediately on any exception (Debug->Exceptions...),
and see what happens when you edit a value in a cell and then move away from
that cell.
 
J

Joanna Carter [TeamB]

"rob" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I have a BindingSource whose DataSource property is set to a list of
| structs (List<MyStruct>). Each property in MyStruct can be read and
| writen (get/set).
|
| I also have a DataGridView to which I manually add columns
| (dataGridView.Columns.Add). Each column has set the property "ReadOnly"
| to false. The DataPropertyName is one of the properties of MyStruct.
| ValueType is set to the type that property, i.e. String, Boolean,
| etc.Finally the DataSource of dataGridView is set to the above
| BindingSource.
|
| All of this works well. I am also able to edit the cells. But as soon
| as I exit the cell, i.e. move to another sell, the changes are reverted
| to the original value. I suspect that something goes wrong with an
| attempted (automatic) synchronization between the datasource, i.e.
| BindingSource, and the new value in the cell. I can't figure out what
| is going on, though. Any help is appreciated.

Structs are passed around as copies, not references. could this be the
problem ?

Try changing your struct to a class.

Joanna
 
D

Dmytro Lapshyn [MVP]

This is a good point, but so do all primitive types (int, double and so on).
And DataGridView doesn't seem to have problems with those. However, the
original poster should definitely consider this possibility.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top