关于将DataGridView与BindingList结合使用,我将禁用编辑当前行,但允许添加新行。我的问题是,当我禁止编辑时,这似乎阻止了它添加新的行项目,因为当您将表格插入该新行的单元格时,它似乎不允许编辑?

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

   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;


谢谢

最佳答案

与其争夺来源,不如要求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# - C#WinForms BindingList和DataGridView-禁止进行编辑会阻止创建新行?我该如何解决?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1519484/

10-09 19:45