【发布时间】:2018-02-19 16:20:13
【问题描述】:
我有一个程序,一旦用户单击按钮,就会向 DataGridView 添加行。我需要将行数限制为最多 10 行。
这是我的代码:
public partial class Form1 : Form
{
private Int32 MaxRows { get; set; }
public Form1()
{
MaxRows = 10;
InitializeComponent();
dataGridView1.UserAddedRow += dataGridView1_RowCountChanged;
dataGridView1.UserDeletedRow += dataGridView1_RowCountChanged;
}
private void dataGridView1_RowCountChanged(object sender, EventArgs e)
{
CheckRowCount();
}
private void CheckRowCount()
{
if (dataGridView1.Rows != null && dataGridView1.Rows.Count > MaxRows)
{
dataGridView1.AllowUserToAddRows = false;
}
else if (!dataGridView1.AllowUserToAddRows)
{
dataGridView1.AllowUserToAddRows = true;
}
}
public void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Rows.Add("This is a row.");
}
}
我从此处发布的另一个问题中获得了代码(似乎找不到链接),但代码不起作用,我能够在我的 DataGridView 中创建超过 11 行。发生这种情况的任何原因?
【问题讨论】:
标签: c# visual-studio oop visual-studio-2013 datagridview