LLBLGenProDataSource, Gridview and Validators

Posts   
 
    
jbliss1234
User
Posts: 42
Joined: 04-May-2007
# Posted on: 05-Mar-2009 22:01:30   

I have a situation where I am binding a gridview (radgrid actually) to an llblgenprodatasource, using boundcolumns. Since these are not template columns, I cannot declaratively use validators, but I have to add the validators to the bound column programmatically. This is done in the grid's item_created event.

I am trying to write a re-usable method to do this. Basically, I have to loop through the fields of the entity and if the field is not nullable, add a required field validator. The question is: can I do this without instantiating a new entity inside this method (see code below), and do this inspection of the fields by somehow automatically looking the llblgenprodatasource. That would be a bit more elegant.

Protected Sub rgForumCategories_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgForumCategories.ItemCreated
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
            Dim item As GridEditableItem = CType(e.Item, GridEditableItem)

            For Each col As GridColumn In e.Item.OwnerTableView.Columns
                If TypeOf (col) Is GridBoundColumn Then
                    Dim gbc As GridBoundColumn = CType(col, GridBoundColumn)

                    'IS THERE ANY WAY TO AVOID CREATING AN INSTANCE OF THE ENTITY
                    Dim di As ForumCategoryEntity = New ForumCategoryEntity
                   'Put in a required field validator if this is a non-nullable field
                    If di.Fields(gbc.DataField).IsNullable = False Then

                        Dim ce As IGridColumnEditor = item.EditManager.GetColumnEditor(col)

                        If TypeOf (ce) Is GridTextBoxColumnEditor Then
                            Dim editor As GridTextBoxColumnEditor = CType(ce, GridTextBoxColumnEditor)
                            Dim cell As TableCell = CType(editor.TextBoxControl.Parent, TableCell)
                            Dim validator As RequiredFieldValidator = New RequiredFieldValidator
                            editor.TextBoxControl.ID = String.Format("txt{0}", gbc.DataField)
                            validator.ControlToValidate = editor.TextBoxControl.ID
                            validator.ErrorMessage = String.Format("{0} is required", gbc.HeaderText)
                            validator.Text = "*"
                            cell.Controls.Add(validator)
                        End If
                    End If
                End If
            Next
        End If
End Sub
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Mar-2009 06:01:42   

The other option is:

new CustomerEntityFactory().CreateFields()["somefield"].IsNullable;
David Elizondo | LLBLGen Support Team