How To: Edit data in a DataGridView Control

G

Guest

I am trying to create an editable grid of data. The source of the data is a
custom collection I have created the collection using generics...

List<myObjectType> myList = new List<MyObjectType>()

Then filled "myList" with objects etc etc.

I can then set the datasource of a BindingSource to "myList". It displays
the data as expected but no matter what properties I set on the BindingSource
or the DataGirdView control, I cannot edit the data.

I have also tried binding the DataGridView directly to the List - no
different.
I have also tried using the BindableList rather than the List, for my
datasource - no luck.

What am I missing? I suspect it's something so basic that all the doco
skips over it, because I can't find any example this simple, except that
maybe this type of datasource is not editable in the first place.

Any help greatly appreciated.
Thanks.
 
O

Otis Mukinfus

I am trying to create an editable grid of data. The source of the data is a
custom collection I have created the collection using generics...

List<myObjectType> myList = new List<MyObjectType>()

Then filled "myList" with objects etc etc.

I can then set the datasource of a BindingSource to "myList". It displays
the data as expected but no matter what properties I set on the BindingSource
or the DataGirdView control, I cannot edit the data.

I have also tried binding the DataGridView directly to the List - no
different.
I have also tried using the BindableList rather than the List, for my
datasource - no luck.

What am I missing? I suspect it's something so basic that all the doco
skips over it, because I can't find any example this simple, except that
maybe this type of datasource is not editable in the first place.

Any help greatly appreciated.
Thanks.
You will have to make the object(s) you are editing use IEditable Interface.

Look up IEditable in the help docs.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
G

Guest

Thanks Otis. I've implemented the IEditableObject interface on the custom
object. The Begin and EndEdit events fire as expected when you select a cell
in the grid, but "what you type" doesn't show in the grid and it doesn't
change the corresponding property of my custom object.

Could this be something to do with AutoGenerateColumns property of the
DataGridView?
 
G

Guest

Oops - thanks again Otis, and please ignore that last post.

IEditable helps greatly as it exposes the edit events I will need to use,
but what helped even more was implementing the "set" methods on the
properties I was trying to edit!!! (fairly embarrased right now...) Anyway,
it is all working as I would expect now and I can move on.

Thanks again for taking the time, and I will make good use of the
IEditableObject interface anyway...

:-(
 
G

Guest

My DataSource is a DataTable or DataSet they should implement IEditable
right? I still cannot get the DataGridView to go into edit mode. Any
suggestions?

Thank you.
 
G

Guest

Start with a few of the less complicated possibilities,
1) Is the DataGridView ReadOnly property set to false.
2) Do you have AllowUsersToAddRows = true (do you need to?)
3) What edit mode is set (see the EditMode property) as this could impact
what you need to do before editing will work.
4) Check the way you load the DataSets or DataTables - do you need to make
sure they are not ReadOnly?

Maybe these will help - sorry if I've over simplified things but sometimes
it doesn't hurt to look for the easiest things first ...

Hope this helps.
Stuart.
 
G

Guest

Thank you. It turns out setting the ReadOnly flag on the DataGridView to
False did the trick for me. Unfortunately the documentation mentions ReadOnly
flags for rows and columns but it did not mention the flag for the grid view
itself.

Thanks again.
 

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