我有一个无界的 DataGridView。我试图消除最后添加的额外行。我已将 AllowUserToAddRows 设置为 false。
// This code is adding two rows at the beginning
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.Rows.Add(new DataGridViewRow());
dataGridView1.NotifyCurrentCellDirty(true);
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
dataGridView1.NotifyCurrentCellDirty(false);
当用户右键单击一行时,我设置了它,他们将能够添加额外的行。但是,如果我右键单击最后一行,它将添加两行。
private void addRowBelowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_selectedRow < dataGridView1.RowCount - 1)
{
dataGridView1.Rows.Insert(_selectedRow + 1, 1);
}
else
{
dataGridView1.Rows.Add();
}
dataGridView1.NotifyCurrentCellDirty(true);
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
dataGridView1.NotifyCurrentCellDirty(false);
}