绑定到列表的可编辑DataGridView [英] Editable DataGridView bound to a List

查看:93
本文介绍了绑定到列表的可编辑DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到列表的DataGridView。值显示正常,当我单击一个值时,它开始编辑,但是当我按Enter键时,更改被忽略,没有数据被更改。当我在Value setter中放置一个断点时,我可以看到它在编辑后执行,但是从未显示任何更改过的数据。我的绑定代码如下所示:

I have a DataGridView bound to a list. The values show fine, when I click on a value, it starts editing, however when I press Enter, the change is ignored, no data are ever changed. When I place a breakpoint in the Value setter, I can see it is executed after the edit, but no changed data are ever shown. My binding code looks like this:

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;
    }
  }
}

该项目是基本的Windows在设计器中创建了DataGrid的Forms项目中,列名为Key和Value,并将DataPropertyName分别设置为Key / Value。

The project is a basic Windows Forms project, with a DataGrid created in a designer, with columns called Key and Value and set DataPropertyName to Key / Value respectively. No values are set to Read Only.

我缺少某些步骤吗?我需要实现 INotifyPropertyChanged 还是其他?

Is there some step I am missing? Do I need to implement INotifyPropertyChanged, or something else?

推荐答案

The问题是您正在使用 struct 作为BindingList项目类型。解决方案是,您应该将 struct 更改为 class ,这样可以很好地工作。但是,如果您想继续使用 struct ,我有一个使之起作用的想法,当然,与简单地更改 struct class 。整个想法是,每当单元格的值发生更改时,都应将基础项目(结构)分配给一个全新的结构项目。这是唯一可用于更改基础值的方法,否则提交更改后的单元格值将不会更改。我发现事件 CellParsing 是适合这种情况下添加自定义代码的事件,这是我的代码:

The problem is your are using a struct as the BindingList item type. The solution is you should change struct to class and it works GREAT. However if you want to keep using struct, I have an idea to make it works, of course it requires more code than simply changing struct to class. The whole idea is every time a cell has its value changed, the underlying item (which is a struct) should be assigned to a totally new struct item. That's the only way you can use to change the underlying value, otherwise the cell value after committing change won't be changed. I've found that the event CellParsing is the one suitable 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};
     }
   }
}

我不认为这是最好继续使用 struct 这样的方式, class 会更好。

I don't think it's a good idea to keep using struct that way, class would be better.

这篇关于绑定到列表的可编辑DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆