C#的WinForms的BindingList和DataGridView中 - 不允许编辑防止新行

2019-07-20 21:52发布

关于我的使用与的BindingList DataGridView的,我是禁用编辑当前行,但允许添加新行。 我的问题是,当我不允许修改,这似乎是阻止人们加入一个新的行项目,因为当你似乎进入表为这个新行的单元格不会允许编辑???

知道如何解决这个问题? 下面我的代码段:

   BindingSource bs = new BindingSource();
   bList = new BindingList<Customer>();
   bList.AllowNew = true;
   bList.AllowEdit = false;

   // Fill bList with Customers
   bList.Add(new Customer("Ted"));
   bList.Add(new Customer("Greg"));
   bList.Add(new Customer("John"));

   bs.DataSource = bList;
   dataGridView1.DataSource = bs;

谢谢

Answer 1:

而不是对抗的来源,也许问DataGridView担任主礼嘉宾:

dataGridView1.DataSource = bs;
dataGridView1.ReadOnly = true;
dataGridView1.CurrentCellChanged += delegate 
{
    DataGridViewRow row = dataGridView1.CurrentRow;
    bool readOnly = row == null ||
        row.Index != dataGridView1.NewRowIndex;
    dataGridView1.ReadOnly = readOnly;
};

(不要设置AllowEdit名单上)



文章来源: C# WinForms BindingList & DataGridView - disallowing EDIT prevents creation of a NEW row? How can I address this?