C# 在开始插入新的DataGridViewRow时,如何删除最后一个DataGridViewRow?
使用Windows窗体应用程序。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;
我有一个类,来自
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
值,这是我试图避免的。