How to update a DataGridView’s Datasource on cell edit

Solution for How to update a DataGridView’s Datasource on cell edit
is Given Below:

I’ve attached my class Reports to a dataGridView’s DataSource through the Select statement below because I don’t want Prop3 showing up in the DGV. I then updated the cellClick event to remove ReadOnly because that seemed to be an issue through initial runs.

When I click a cell and try to change a value to 123 it switches back to 456 once I hit enter. I’ve attempted to use various other events such as CellEndEdit, CellValueChanged, and any other event I thought may help to no luck. I cannot even find the value (123) I’m attempting to change to stored anywhere while running the code with breakpoints. I want to make sure that the value in myClass gets set to 123 after the cell edit has completed. If I don’t use the select statement then all 3 properties will display in the DGV and I can then edit values the way I’m asking to do, but again I don’t want Prop3 showing so used the select statement.

    List<MyClass> myClass;
    public Form3()
    {
        InitializeComponent();

        myClass = new List<MyClass>();
        for (int i = 0; i < 2; i++)
        {
            myClass.Add(new MyClass(456, 456));
        }
        dataGridView1.DataSource = myClass.Select(x => new { x.Prop1, x.Prop2 }).ToList();
        dataGridView1.ReadOnly = false;
        dataGridView1.CellClick += DataGridView1_CellClick;
    }

    private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        (sender as DataGridView).CurrentCell.ReadOnly = false;

    }

    public class MyClass
    {
        public int Prop1{get;set;}
        public int Prop2 {get;set;}
        public int Prop3 {get;set;}
        public MyClass(int prop1, int prop2)
        {
            Prop1 = prop1;
            Prop2 = prop2;
        }
        public MyClass() { }
    }

because I don’t want Prop3 showing up in the DGV

It’s a lot simpler to just remove the column from the DGV after it’s generated it

var x = new List<MyClass>(){
  new MyClass(){ Prop1 = 1, Prop2 = 22, Prop3 = 4015559191 },
  new MyClass(){ Prop1 = "Fred", Prop2 = 23, Prop3 = "4015559002" },
};

Appreciate that by default a DataGridView’s AutoGenerateColumns property is true. When you bind to a list of MyClass, DGV will inspect the properties of MyClass and make columns for them, taking the datatype and name into account

Nothing stops you editing its creations afterwards:

dataGridView1.DataSource = x;

dataGridView1.AutogenerateColumns = false; //turn it off so it doesn't later recreate removed columns
dataGridView1.Columns.Remove("Prop3"); //Prop3 property became a column named Prop3

I want to make sure that the value in myClass gets set to 123 after the cell edit has completed.

But, the grid isn’t bound to myClass; you created a new, different, totally separate List elsewhere in memory when you did ToList() in myClass.Select(...).ToList() and bound the grid to the new list.

Worse, in the Select you made new anonymous typed object and copied the data contents of the properties of your MyClass items over, which fully disconnected the instances of MyClass stored in myClass and bound the grid to a list full of anonymous types that just happened to have copies of the data

When I click a cell and try to change a value to 123 it switches back to 456 once I hit enter

The straw that breaks the camel’s back is that anonymous types are immutable – you’ve bound your grid to a brand new list, full of brand new read only anonymous types..

..the grid could never influence the contents of myClass