如何限制DataGridView让用户手动添加的行数?

时间:2021-05-20 14:47:43
用户可以手动添加新行,但只能添加4行,满4行后不再自动生成新行.

5 个解决方案

#1



     private void Form1_Load(object sender, EventArgs e)
        {        
            dataGridView1.UserAddedRow+=new DataGridViewRowEventHandler(dataGridView1_UserAddedRow);
        }
        private int m_AddCount = 0;
        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            m_AddCount++;
            if (m_AddCount == 4) dataGridView1.AllowUserToAddRows = false;
        }

#2


做一个循环事件在DataGridView里的RowsAdded事件

for(int x=0;x<dgv.Rows.count; x++)
{
  if(x>3)
    关闭
  else
   打开
}

#3



 dgv.RowsAdded += new DataGridViewRowsAddedEventHandler(dgvEvent);
 dgv.RowsRemoved += new DataGridViewRowsAddedEventHandler(dgvEvent);

private void dgvEnvet()
{
for(int x=0;x<dgv.Rows.count; x++)
{
  if(x>3)
   dgv.AllowUserToAddRows = false;

  else
    dgv.AllowUserToAddRows = true;
}


}

#4



        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            if (dataGridView1.RowCount > 4)
                dataGridView1.AllowUserToAddRows = false;
        }

        private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            for (int i = dataGridView1.RowCount-1; i >=0; i--)
            {
                dataGridView1.Rows[i].Cells["土层编号"].Value = i + 1;
            }

            if (dataGridView1.RowCount < 4)
                dataGridView1.AllowUserToAddRows = true;
            
        }

#5


只能用强这个字来附会;
为什么C#.net 不提供充足的事件,行添加之前的事件,硬是找不到;
谁来,找到一个类似的事件;
没有文献来说清楚他的事件链,这门语言没救了;

#1



     private void Form1_Load(object sender, EventArgs e)
        {        
            dataGridView1.UserAddedRow+=new DataGridViewRowEventHandler(dataGridView1_UserAddedRow);
        }
        private int m_AddCount = 0;
        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            m_AddCount++;
            if (m_AddCount == 4) dataGridView1.AllowUserToAddRows = false;
        }

#2


做一个循环事件在DataGridView里的RowsAdded事件

for(int x=0;x<dgv.Rows.count; x++)
{
  if(x>3)
    关闭
  else
   打开
}

#3



 dgv.RowsAdded += new DataGridViewRowsAddedEventHandler(dgvEvent);
 dgv.RowsRemoved += new DataGridViewRowsAddedEventHandler(dgvEvent);

private void dgvEnvet()
{
for(int x=0;x<dgv.Rows.count; x++)
{
  if(x>3)
   dgv.AllowUserToAddRows = false;

  else
    dgv.AllowUserToAddRows = true;
}


}

#4



        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            if (dataGridView1.RowCount > 4)
                dataGridView1.AllowUserToAddRows = false;
        }

        private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            for (int i = dataGridView1.RowCount-1; i >=0; i--)
            {
                dataGridView1.Rows[i].Cells["土层编号"].Value = i + 1;
            }

            if (dataGridView1.RowCount < 4)
                dataGridView1.AllowUserToAddRows = true;
            
        }

#5


只能用强这个字来附会;
为什么C#.net 不提供充足的事件,行添加之前的事件,硬是找不到;
谁来,找到一个类似的事件;
没有文献来说清楚他的事件链,这门语言没救了;