how to set maximum number of rows in datagridview
  • is there anyway for set max number of rows for datagridview because i want to set that user can enter only maximum 3 rows in datagridview not morethan  rows how can i handle limit number of rows for datagridview.
  • 2 Comments sorted by
  • Vote Up0Vote Down WaqasWaqas
    172.00 Karma Accepted Answer
    use UserAddedRow and UserDeltedRow Event of datagridview for handle max rows in datagridview and adjust below code.




    int MaxRowCount = 3;

    private void dgUser_UserAddedRow(object sender, DataGridViewRowEventArgs e)
    {
    if (dataGridView1.Rows.Count > MaxRowCount)
    {
    dataGridView1.AllowUserToAddRows = false;
    }
    else
    {
    dataGridView1.AllowUserToAddRows = true;
    }
    }

    private void dgUser_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
    {
    if (dataGridView1.Rows.Count > MaxRowCount)
    {
    dataGridView1.AllowUserToAddRows = false;
    }
    else
    {
    dataGridView1.AllowUserToAddRows = true;
    }
    }
  • Thank u Waqas the code works like a charm :)
    hussainroyal

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

    Optimum Creative