toomasz
toomasz

Reputation: 45

Editing list properties using DataGridview

Ok, I have my custom class:

    public class FileItem : INotifyPropertyChanged
    {
        int id=0;
        string value="";
        public int Id
        {
            get { return id; }
            set { id = value; Changed("Id"); }
        }
        public string Value
        {
            get { return value; }
            set { this.value = value; Changed("Value"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        void Changed(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

 public BindingList<FileItem> FilesystemEntries = new BindingList<FileItem>();

And I have DatagridView1 with DataSource set to FilesystemEntries:

 binding.DataSource = FilesystemEntries;

Already I can Add and remove rows - these chnages are reflected on collection. However, Value and Id are not saved into bidning list when i change them in DataGridView, id is always 0 and value is "".

How can I make this work? Do I need to implement some interface to FileItem to allow editing properties?

ReadOnly of DGV is set to false, same to all columns. Editing, Deleting and Changing are enabled.

Upvotes: 0

Views: 1600

Answers (2)

toomasz
toomasz

Reputation: 45

Problem solved, it was not working because i set AutoGenerateColumns to false, and I added two columns in which i forgot to set DataSource property. Now its working.

Upvotes: 1

Zach Johnson
Zach Johnson

Reputation: 24212

Have you clicked away from the editing cell to let the DataGridView know you are done editing the cell? By default, the cell's value will not be pushed to the underlying object until you click away from it.

Upvotes: 0

Related Questions