Click here to Skip to main content
16,019,593 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day Please i have a datagridview and i use it enter 4 informations at once but dont want the datagridview to exceed more than 4 rows (i dont want the user to enter more than 4 rows at once).If the rows are more than four the datagridview should not extend any longer

What I have tried:

if (dataGridView1.Rows.Count >= 4)
            {
                MessageBox.Show("you cant exceed this number");
            }
Posted
Updated 19-Oct-17 17:18pm
Comments
PIEBALDconsult 19-Oct-17 20:37pm    
I doubt you want to use a datagridview at all. You ought to re-think.

1 solution

use DataGridView.UserAddedRow Event (System.Windows.Forms)[^]

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
       {
           if (dataGridView1.Rows.Count > 4)
           {
               dataGridView1.AllowUserToAddRows = false;
               MessageBox.Show("you cant exceed this number");
           }
           else
           {
               dataGridView1.AllowUserToAddRows = true;

           }

       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900