Setting maximum number of rows in DataGridView?

10,607

Solution 1

CheckRowCount() can be simplified. You just need to disable AllowUserToAddRows only when the max limit has reached.

private void CheckRowCount()
    {
        // The data grid view's default behavior is such that it creates an additional row up front.
        // e.g. when you add 1st row, it creates 2nd row automatically.
        // If you use Count < MaxRows, the user won't be able to add the 10th row.
        if (dataGridView1.Rows.Count <= MaxRows)
        {
            dataGridView1.AllowUserToAddRows = true;
        }
        else
        {
            dataGridView1.AllowUserToAddRows = false;
        }
    }

When you add a new row on button click, make sure the current row count is less than the MaxRows. You should also call the CheckRowCount method to make sure the AllowUserToAddRows is set accordingly. Modify the button click handler as follows:

private void button1_Click(object sender, EventArgs e)
    {
        // If the rows count is less than max rows, add a new one.
        if (dataGridView1.Rows.Count < MaxRows)
        {
            this.dataGridView1.Rows.Add("This is a row.");

            // Check the row count again.
            CheckRowCount();
        }
    }

Solution 2

Maybe this way is easier?

private Int32 MaxRows { get; set; }

public Form1()
{
    MaxRows = 10;
    InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{
    if (dataGridView1.Rows.Count <= MaxRows)
    this.dataGridView1.Rows.Add("This is a row.");
}

Also, AllowUserToAddRows property is: "true if the add-row option is displayed to the user; otherwise false. The default is true" has nothing to do with the adding of rows from code behind from where you are adding rows. It refers to the option to a new rows into the grid by clicking into the data grid while your app is running.

Share:
10,607

Related videos on Youtube

thepersonwho
Author by

thepersonwho

Web Developer Intern

Updated on June 04, 2022

Comments

  • thepersonwho
    thepersonwho almost 2 years

    I have a program that adds rows to the DataGridView once the user clicks a button. I need to limit the number of rows to a maximum of 10.

    Here's my code:

    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.");
        }
    
    }
    

    I got the code from another question posted here (can't seem to find the link), but the code does not work and I'm able to make over 11 rows in my DataGridView. Any reason as to why this is happening?

  • Norbert Forgacs
    Norbert Forgacs over 6 years
    And if you need some visual feedback that you can't add more rows, you should only disable your button while your datagrid has 10 rows.