Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在开始插入新的DataGridViewRow时,如何删除最后一个DataGridViewRow?_C#_Winforms_Exception_Datagridview_Datagridviewrow - Fatal编程技术网

C# 在开始插入新的DataGridViewRow时,如何删除最后一个DataGridViewRow?

C# 在开始插入新的DataGridViewRow时,如何删除最后一个DataGridViewRow?,c#,winforms,exception,datagridview,datagridviewrow,C#,Winforms,Exception,Datagridview,Datagridviewrow,使用Windows窗体应用程序。 我有一个类,来自DataGridView控件: public class CustomDataGridView : DataGridView { private int maxRowsAllowed = 3; public CustomDataGridView() { this.AutoGenerateColumns = false; this.AllowUserToAddRows = false;

使用Windows窗体应用程序。
我有一个类,来自
DataGridView
控件:

public class CustomDataGridView : DataGridView
{
    private int maxRowsAllowed = 3;

    public CustomDataGridView()
    {
        this.AutoGenerateColumns = false;
        this.AllowUserToAddRows = false;
        this.AllowUserToDeleteRows = false;
        this.ReadOnly = true;
        this.RowsAdded += CustomDataGridView_RowsAdded;
        this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    }

    public void Start()
    {
        this.Columns.Add("col1", "header1");
        this.Columns.Add("col2", "header2");

        // rows added manually, no DataSource
        this.Rows.Add(maxRowsAllowed);
    }

    private void customDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        // At this point, while debugging, I realized that CurrentRow is null,
        // doing impossible to change it to a previous one, this way avoiding an exception.
        if (this.Rows.Count > maxRowsAllowed)
        {
            this.Rows.RemoveAt(maxRowsAllowed);
        }
    }
}
然后,从容器类的
addRowatStart
方法中,在0索引处插入一个新行,将一个索引向下移动其他索引。
当引发
RowsAdded
事件,并且仅当实际总行数大于
rowsAllowed
时,最后一个将被删除

public class ContainerForm : Form
{
    private CustomDataGridView dgv;

    public ContainerForm()
    {
        InitializeComponent();

        dgv = new CustomDataGridView();

        dgv.Size = new Size(400, 200);
        dgv.Location = new Point(10, 10);
        this.Controls.Add(dgv);

        dgv.Start();
    }

    // Inserts a row at 0 index
    private void aButton_Click(object sender, EventArgs e)
    {
        var newRow = new DataGridViewRow();
        newRow.DefaultCellStyle.BackColor = Color.LightYellow;

        dgv.Rows.Insert(0, newRow);
    }
}
一切正常,直到由于位移而选择删除
CurrentRow
(标题上有小箭头)

public class ContainerForm : Form
{
    private CustomDataGridView dgv;

    public ContainerForm()
    {
        InitializeComponent();

        dgv = new CustomDataGridView();

        dgv.Size = new Size(400, 200);
        dgv.Location = new Point(10, 10);
        this.Controls.Add(dgv);

        dgv.Start();
    }

    // Inserts a row at 0 index
    private void aButton_Click(object sender, EventArgs e)
    {
        var newRow = new DataGridViewRow();
        newRow.DefaultCellStyle.BackColor = Color.LightYellow;

        dgv.Rows.Insert(0, newRow);
    }
}
我想,这就是当
RowsAdded
转义时抛出
System.ArgumentOutOfRangeException
的原因,尝试返回
dgv.Rows.Insert(0,newRow)

我还找不到任何解决方案。

尝试更改此设置

if (this.Rows.Count > maxRowsAllowed)
{
    this.Rows.RemoveAt(maxRowsAllowed);
}
对此

if (this.Rows.Count > maxRowsAllowed)
{
    // if the number of rows is 10
    // the index of the last item is 9
    // index 10 is out of range
    this.Rows.RemoveAt(maxRowsAllowed -1);
}

不,这不是问题的原因。如果Row.Count大于10(maxRowsAllowed),datagridview必须至少有11行,
this.rows.RemoveAt(10)
不会抛出argumentoutofrangeexception。虽然很容易绕过这个问题,但我不知道它的根本原因;修复方法之一是将
customDataGridView\u RowsAdded
的代码移动到
aButton\u Click
方法的末尾,在
dgv.Rows.Insert(0,newRow)之后插入
。当然,您的建议是正确的,但这样,
ContainerForm
必须知道
maxrowsallow
值,这是我试图避免的。