Contents tagged with RadGrid

  • A Small World in ASP.NET–NimblePros Acquired by Telerik

    A few weeks ago, fellow ASPInsider Steve Smith of NimblePros passed a client to me. It’s a part-time “tweaks and maintenance” gig for a small startup. I was quite happy to take this on. I’m mostly retired and no longer work fulltime but still want to keep my hand in ASP.NET and Visual Studio - and earn money for toys.


  • Extracting the Date from a DateTime in Entity Framework 4 and LINQ

    In my current ASP.NET 4 project, I’m displaying dates in a GridDateTimeColumn of Telerik’s ASP.NET Radgrid control. I don’t care about the time stuff, so my DataFormatString shows only the date bits:

    <telerik:GridDateTimeColumn FilterControlWidth="100px"
      DataField="DateCreated" HeaderText="Created" 
      SortExpression="DateCreated" ReadOnly="True" 
      UniqueName="DateCreated" PickerType="DatePicker" 
      DataFormatString="{0:dd MMM yy}">

    My problem was that I couldn’t get the built-in column filtering (it uses Telerik’s DatePicker control) to behave.  The DatePicker assumes that the time is 00:00:00 but the data would have times like 09:22:21. So, when you select a date and apply the EqualTo filter, you get no results. You would get results if all the time portions were 00:00:00.

    In essence, I wanted my Entity Framework query to give the DatePicker what it wanted… a Date without the Time portion. Fortunately, EF4 provides the TruncateTime  function. After you include

    Imports System.Data.Objects.EntityFunctions

    You’ll find that your EF queries will accept the TruncateTime function. Here’s my routine:

    Protected Sub RadGrid1_NeedDataSource _
        (ByVal source As Object, _
         ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) _
        Handles RadGrid1.NeedDataSource
        Dim ent As New OfficeBookDBEntities1
        Dim TopBOMs = From t In ent.TopBom, i In ent.Items _
                      Where t.BusActivityID = busActivityID _
          And i.BusActivityID And t.ItemID = i.RecordID _
          Order By t.DateUpdated Descending _
          Select New With {.TopBomID = t.TopBomID, .ItemID = t.ItemID, _
                           .PartNumber = i.PartNumber, _
                           .Description = i.Description, .Notes = t.Notes, _
                           .DateCreated = TruncateTime(t.DateCreated), _
                           .DateUpdated = TruncateTime(t.DateUpdated)}
        RadGrid1.DataSource = TopBOMs
    End Sub

    Now when I select March 14, 2011 on the DatePicker, the filter doesn’t stumble on time values that don’t make sense.

    Full Disclosure: Telerik gives me (and other developer MVPs) free copies of their suite.


  • Fixing EntityDataSourceWrapper Error in EF (VB Version)

    I lost a lot of time today on this error:

    ‘EntityDataSourceWrapper' does not contain a property with the name …'

    The project is ASP.NET 3.5 using Entity Framework and VB. I’m using the Telerik RadControls for ASP.NET AJAX (a sweet suite!). In my app, clicking a row’s Edit button on the RADGrid  opens the popup Edit form for updating the data.

    It seemed like a simple task to get the DataItem for the row (item), extract a value, and use that value as the SelectedValue of radiobuttonlist.  It turns out that you need heroic measures to get a value out of e.Item.DataItem in Entity Framework! Sheesh!

    Here’s the VB version of LinqHelpers as posted by Diego B Vega. It noses around in the DataItem to return a TEntity (whatever that is!). I’m still trying to figure this code out, but at least I’m productive again!

    ' LinqHelpers.vb - Put it into the App_Code folder
    Namespace LinqHelpers
        Public NotInheritable Class EntityDataSourceExtensions
            Public Shared Function GetItemObject _
            (Of TEntity As Class)(ByVal dataItem As Object) As TEntity
                Dim entity As TEntity = TryCast(dataItem, TEntity)
                If entity IsNot Nothing Then
                    Return entity
                End If
                Dim td = TryCast(dataItem, ICustomTypeDescriptor)
                If td IsNot Nothing Then
                    Return DirectCast(td.GetPropertyOwner(Nothing), TEntity)
                End If
                Return Nothing
            End Function
        End Class
    End Namespace

    Once you’ve got a way to mess around inside the DataItem, you can fix up the radiobuttonlist (a dropdownlist would be similar). Do this in the RadGrid’s ItemCreated event as shown:

    Protected Sub RadGrid1_ItemCreated _
      (ByVal sender As Object, ByVal e As  _
       Telerik.Web.UI.GridItemEventArgs) _
       Handles RadGrid1.ItemCreated
        ' Sets the selected values for radiobuttons and
        ' ddls when displaying the edit form
        If (TypeOf e.Item Is GridEditFormItem) And _
            e.Item.IsInEditMode And _
            (Not IsNothing(e.Item.DataItem)) Then
            ' Get a reference to the editformitem
            Dim editFormItem As GridEditFormItem = _
            CType(e.Item, GridEditFormItem)
            ' Get the entity that's being used
            'with this screen (requires a LINQ helper)
            Dim entity = LinqHelpers.EntityDataSourceExtensions. _
            GetItemObject(Of CMS_Item)(e.Item.DataItem)

            ' Get a reference to the radiobutton list
            ' and set its value
            Dim rbl As RadioButtonList = _
            rbl.SelectedValue = entity.ItemType

            ' Get a reference to the Product radiobutton
            ' and set its value
            rbl = editFormItem.FindControl("radProduct")
            rbl.SelectedValue = entity.Product.ToString()

            ' Set the Stocked radiobuttonlist
            rbl = editFormItem.FindControl("radStocked")
            rbl.SelectedValue = entity.Stocked.ToString()

         End If
    End Sub

    Thanks to all the community contributions that help solve these issues. Here’s a thread with an answer by Microsoft’s Diego B Vega that got me over the hump:

    Am I the only one who spins on these things???