Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..

My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.

Any idea?

Dani AI

Generated

Brief summary and practical pattern for ASP.NET (WebForms) DataGrid inserts — builds on the replies from , , and but clarifies what actually applies to WebForms and gives a working approach.

Note on events: the cell-level events mentioned in the thread are typically from Windows Forms grids and do not exist on ASP.NET WebForms DataGrid. For WebForms, perform validation in the server handler that processes the insert (for example the ItemCommand handler that responds to an Insert/Add button in a FooterTemplate or the button click inside your insert template). Client-side checks are fine for user experience but cannot replace server validation.

Pattern to follow (server-side first, client-side for UX):

  • Retrieve the inserting controls from the template (use e.Item.FindControl or DataGrid.Footer.FindControl).
  • Perform strict checks: required, type parsing with TryParse, range checks, regex where needed.
  • If any check fails, set an error label and abort the insert (do not call the DB).
  • If all checks pass, use parameterized SQL or a stored procedure to insert; also enforce constraints at the DB level.

Example VB.NET snippet (validate in ItemCommand before inserting):

Protected Sub MyDataGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
    If e.CommandName <> "Add" Then Return

    Dim txtName As TextBox = CType(e.Item.FindControl("txtNameFooter"), TextBox)
    Dim txtQty As TextBox = CType(e.Item.FindControl("txtQtyFooter"), TextBox)
    Dim qty As Integer

    If String.IsNullOrWhiteSpace(txtName.Text) Then
        lblError.Text = "Name is required."
        Return
    End If

    If Not Integer.TryParse(txtQty.Text, qty) Then
        lblError.Text = "Quantity must be a whole number."
        Return
    End If

    ' All validations passed — proceed to insert using parameterized command
End Sub

Extra tips

  • Add ASP.NET validators (RequiredFieldValidator, RegularExpressionValidator, CustomValidator) with ValidationGroup for quick client feedback; always re-check on server.
  • Use a central validation helper if multiple places insert data.
  • Prevent SQL injection with parameters and rely on DB constraints (unique/index checks) as last defense.
  • If controls are in templates, double-check IDs and use the correct DataGridItemType (Footer vs Insert template) when finding controls.

Recommended Answers

All 3 Replies

maybe you can use the keypress function

Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..

My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.

Any idea?

With the CellValidating and CellValidated events. :)

just check the value of the control that is used for every function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.