2

关于我将 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;

谢谢

4

1 回答 1

4

与其与消息来源抗争,不如要求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在列表中设置)

于 2009-10-05T11:33:25.200 回答