This is a migrated thread and some comments may be shown as answers.

Add multiple rows based on field validation

1 Answer 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vijay
Top achievements
Rank 1
Vijay asked on 27 Jan 2014, 12:48 AM
Hi,

I want to add (create and add to my datasource) multiple rows to my grid based on validating a field in the 'add new row' row.
At present, when I do this (as described below) it leaves me with the row being added. I would like to cancel the added row. In doing this I have been wondering if I am going about it all incorrectly, however:

I am unsure if I should perform some other function that captures the Add New Row functionality, if I should be doing what I'm doing (validating on CellValidating and adding the new rows) or using CellEndEdit or some other function to perform the load and cancelling the original edit.

In the grid (ProductCode, Operation, Quantity, Hours, and some resultant fields), I am trying to allow the user to enter in a specific sub-product code - to enter in one line, or to enter in a parent product code which will pre-populate a bunch of rows (for all the children meeting the criteria).

It currently is working as I want, barring the cancelEdit usage after adding in bulk. I'm not sure how to accomplish this (or do it better). Any advice would be really appreciated!

Here is basically what I currently have:
Private Sub rgvProducts_CellValidating(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles rgvProducts.CellValidating
        Dim col As GridViewDataColumn = e.Column
        Dim validateMessage As String = ""
        lblMsg.Text = ""
 
        If quitValidating Then
            Return
        End If
 
        If e.Row IsNot Nothing Then
            'if new row and field entered is "ProductCode"
            If e.Row.GetType Is GetType(GridViewNewRowInfo) AndAlso col.FieldName = "ProductCode" Then
                If String.IsNullOrWhiteSpace(e.Value) Then
                    'show validation message: cannot be blank
                    Return
                End If
 
                Dim coreProducts As DataTable
                Dim lp As LabourProduct
                Dim resultsMessage As String = ""
 
                'Check if ProductCode value is valid against CoreProduct mapping
                If ApplicationFunctions.fieldValidate("CoreProductCode", e.Value) Then
                    coreProducts = Labour.retrieveCoreProductRows("", e.Value, cl.Operation) 'retrieve set of CoreProduct rows filtered by Operation
                    If coreProducts.Rows.Count = 0 Then
                        'show validation message: No CoreProduct items found
                        Return
                    ElseIf coreProducts.Rows.Count = 1 Then 'if single row returned, allow editing in-line
                        rgvProducts.CurrentRow.Cells("ProductCode").Value = coreProducts.Rows(0)("CoreProductCode")
                        rgvProducts.CurrentRow.Cells("Operation").Value = coreProducts.Rows(0)("Operation")
                        rgvProducts.CurrentRow.Cells("ActualQuantity").Value = coreProducts.Rows(0)("StandardQuantity")
                        rgvProducts.CurrentRow.Cells("StandardQuantity").Value = coreProducts.Rows(0)("StandardQuantity")
                        rgvProducts.CurrentRow.Cells("StandardHours").Value = coreProducts.Rows(0)("StandardHours")
                        rgvProducts.CurrentRow.Cells("EarnedHours").Value = coreProducts.Rows(0)("StandardHours")
                        rgvProducts.CurrentRow.Cells("Description").Value = coreProducts.Rows(0)("ProductDescription")
                        Return
                    Else
                        resultsMessage = ProcessCoreProductDataTable(coreProducts) 'This adds multiple rows to grid and saves the objects
                        If Not resultsMessage = "Success" Then
                            'show validation message: Error saving
                            Return
                        Else 'Here trying to rebind the data source and cancel the edit
                            ApplicationFunctions.showMsg(lblMsg, resultsMessage, ApplicationFunctions.AlertModes.Positive)
                            rgvProducts.DataSource = ""
                            rgvProducts.DataSource = cl.LabourProductList
                            rgvProducts.CancelEdit()
                        End If
                    End If
                Else 'if not matched against the CoreProduct, then trying to match against Product parent
                    coreProducts = Labour.retrieveCoreProductRows(e.Value, "", cl.Operation)
                    If coreProducts.Rows.Count > 0 Then 'if rows returned then add them
                        resultsMessage = ProcessCoreProductDataTable(coreProducts)
                        If Not resultsMessage = "Success" Then
                            ApplicationFunctions.showMsg(lblMsg, resultsMessage, ApplicationFunctions.AlertModes.Negative)
                            e.Row.ErrorText = resultsMessage
                            e.Cancel = True
                            Return
                        Else
                            ApplicationFunctions.showMsg(lblMsg, resultsMessage, ApplicationFunctions.AlertModes.Positive)
                            rgvProducts.DataSource = ""
                            rgvProducts.DataSource = cl.LabourProductList
                            rgvProducts.CancelEdit()
                        End If
                    'else show validation message: product code entered invalid
                    End If
                End If
            ElseIf (e.Row.GetType Is GetType(GridViewDataRowInfo) Or e.Row.GetType Is GetType(GridViewNewRowInfo)) And Not col Is Nothing Then 'only inline editing for any already entered rows
                validateMessage = validate(col.FieldName, e.Value)
                If validateMessage <> String.Empty Then ApplicationFunctions.showMsg(lblMsg, validateMessage, ApplicationFunctions.AlertModes.Negative)
            End If
        End If
    End Sub
     
     
    'add items in bulk and save
    Private Function ProcessCoreProductDataTable(ByRef coreProducts As DataTable) As String
        Dim saveMessage As String = ""
        Dim lp As LabourProduct
        For Each cp As DataRow In coreProducts.Rows
            lp = New LabourProduct()
            lp.LabourID = cl.LabourID
            lp.ProductCode = cp("CoreProductCode")
            lp.CoreBoxNumber = lp.ProductCode.Substring(lp.ProductCode.LastIndexOf("_C"))
            lp.Operation = cp("Operation")
            lp.ActualQuantity = cp("StandardQuantity")
            lp.StandardQuantity = cp("StandardQuantity")
            lp.StandardHours = cp("StandardHours")
            lp.EarnedHours = cp("StandardHours")
            lp.Description = cp("ProductDescription")
            If Not lp.save(saveMessage) Then
                Return saveMessage
            Else
                cl.LabourProductList.Add(lp)
            End If
        Next
        Return saveMessage
    End Function

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 29 Jan 2014, 04:06 PM
Hi Vijay,

Thank you for contacting us.

If you want to stop the user from adding new rows from the NewRow of RadGridView then you can use the UserAddingRow event, which can be canceled. Then you can add your own rows.

Let me know if you need further assistance.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Vijay
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or