Editable list-bound DataGridView

I have a DataGridView associated with a list. The values ​​are displayed perfectly, when I click on a value, it starts editing, however, when I press Enter, the change is ignored, the data does not change. When I set a breakpoint in a set of values, I see that it is executed after editing, but no changed data is ever displayed. My binding code is as follows:

namespace DataGridViewList { public partial class Form1 : Form { public struct LocationEdit { public string Key { get; set; } private string _value; public string Value { get { return _value; } set { _value = value; } } }; public Form1() { InitializeComponent(); BindingList<LocationEdit> list = new BindingList<LocationEdit>(); list.Add(new LocationEdit { Key = "0", Value = "Home" }); list.Add(new LocationEdit { Key = "1", Value = "Work" }); dataGridView1.DataSource = list; } } } 

The project is a basic Windows Forms project with a DataGrid created in the designer, with columns called "Key and Value" and setting DataPropertyName for Key / Value, respectively. There are no read-only values.

Is there any step that I am missing? Do I need to implement INotifyPropertyChanged or something else?

+4
source share
1 answer

The problem is that you are using struct as the type of the BindingList element. The solution is that you have to change the struct to a class , and it works BIG. However, if you want to continue to use struct , I have an idea to get it working, of course, this requires more code than just changing the struct to class . The whole idea is that every time a cell changes its value, the basic element (which is the structure) must be attached to a completely new element of the structure. This is the only way to change the base value, otherwise the cell value will not be changed after the change. I found that the CellParsing event is appropriate for this case to add custom code, and here is my code:

 namespace DataGridViewList { public partial class Form1 : Form { public struct LocationEdit { public string Key { get; set; } private string _value; public string Value { get { return _value; } set { _value = value; } } }; public Form1() { InitializeComponent(); BindingList<LocationEdit> list = new BindingList<LocationEdit>(); list.Add(new LocationEdit { Key = "0", Value = "Home" }); list.Add(new LocationEdit { Key = "1", Value = "Work" }); dataGridView1.DataSource = list; } //CellParsing event handler for dataGridView1 private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e){ LocationEdit current = ((BindingList<LocationEdit>)dataGridView1.DataSource)[e.RowIndex]; string key = current.Key; string value = current.Value; string cellValue = e.Value.ToString() if (e.ColumnIndex == 0) key = cellValue; else value = cellValue; ((BindingList<LocationEdit>)dataGridView1.DataSource)[e.RowIndex] = new LocationEdit {Key = key, Value = value}; } } } 

I don’t think it’s a good idea to continue using struct this way, class would be better.

+3
source

Source: https://habr.com/ru/post/1483301/


All Articles