사용자가 단추를 클릭하면 DataGridView에 행을 추가하는 프로그램이 있습니다. 내가 여기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 행 이상을 만들 수 있습니다. 왜 이런 일이 벌어지는 지 이유는 무엇입니까?
을 그리고 당신은 당신이 더 많은 행을 추가 할 수있는 몇 가지 시각적 피드백이 필요한 경우 데이터 그리드가 10 개 행을 가지고있는 동안, 당신은 단지 당신의 버튼을 해제해야합니다 다음과 같이 버튼을 클릭 핸들러를 수정합니다. – berthos