DataGridView中的AddRowEvent添加额外行

时间:2014-10-17 14:45:30

标签: .net datagridview datagridviewrow

我有一个无界的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);
}

1 个答案:

答案 0 :(得分:0)

在DataGridView中是一个名为“NewRowNeeded”的事件。因此,如果调用newrowneeded,则只需设置

this.dataGridView1.AllowUserToAddRows = true;

并添加一些行:

dataGridView1.Rows.Add();
希望这会对你有所帮助!见到你!