views:

367

answers:

1

I have a UI that uses datagridviews / bindingsource / datatables of typed dataset for data entry. The dataset itself is serialized to a varbinary(max) in SQL. (i.e. no tableadapter, backend schema)

I would like to limit the number of rows the user can enter into some of the grids (the data is used to fill PDF forms and I don't want them entering more rows than the forms can accomodate.)

I have subclassed the datagridview, added a rowlimit property and tried to manipulate the AllUsertoAddRows property

Imports System.Windows.Forms

Public Class dgv
Inherits System.Windows.Forms.DataGridView

Private _Rowlimit As Int32
Public Property Rowlimit() As Int32
    Get
        Return _Rowlimit
    End Get
    Set(ByVal value As Int32)
        _Rowlimit = value
    End Set
End Property
Private Sub dgv_RowLeave(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DataGridViewCellEventArgs) Handles Me.RowLeave

    If Me.Rowlimit > 0 And Me.RowCount > Me.Rowlimit Then

        MessageBox.Show("Maximun records (" & Me.Rowlimit & ") reached.")

        Me.AllowUserToAddRows = False

    Else
        Me.AllowUserToAddRows = True
    End If

End Sub

Private Sub dgvContainer_RowsRemoved(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles Me.RowsRemoved

    If Me.Rowlimit = 0 Or Me.RowCount < Me.Rowlimit Then

        Me.AllowUserToAddRows = True

    End If

End Sub

End Class

The behavior I see is that the messagebox comes up after leaving the max row even when the user is trying to leave the grid. I can lose the messagebox and deal with notification some other way but I thought someone else may have come up with something a little more sophisticated to handle simply causing attempting to add to many records to navigate out of the grid to the next UI control.

Would appreciate any other suggestions as to a more elegant way to approach this.

TIA

A: 

What about checking the number of rows within the RowsAdded event handler?

Another option is to add in the maximum number of rows allowable into the DGV at the start, prevent the user from adding more, and checking to see whether information has been entered or not when you're ready to commit the input.

John at CashCommons