Monday, September 22, 2008

How to limit the number of rows to enter in DataGridView

In a DataGridView if the number of rows should be limited to a
predefined value, we can switch AllowUserToAddRows to "true"
or "false", it will let the user be able to add new rows or stop
the user for entering more new rows.

RowLeave event is the place to count number of Rows and if it's
equal to limit, then we need to stop adding more. on the other hand
if user delete some row(s), it opens new room for adding new ones.

In the following part of code I considered to have just 20 rows in
our DataGridView control:


private void dgv1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
  if (dgv1.Rows.Count >= 20)
      dgv1.AllowUserToAddRows = false;
}
private void dgv1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
  if (dgv1.Rows.Count < 20)
      dgvTmp.AllowUserToAddRows = true;
}

Share/Bookmark

1 comment:

MYSTICODF said...

excelente me sacaste de un apuro