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

RadGrid Filtering Nullable ENUMS

2 Answers 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Claes
Top achievements
Rank 1
Claes asked on 10 Feb 2011, 08:53 AM

I had a problem that I just could not understand why it would not work.
I use enums at times to populate dropdowns. For an example

[DataContract]
[Serializable]

public enum Corporate
{
  [EnumMember] [Description("FD")] COMPANY,
  [EnumMember] [Description("IB")] INDUSTRY,
  [EnumMember] [Description("KD")] PRIVATE,
  [EnumMember] [Description("SLE")] SLEIPNER
}

I want Company, Private etc to be populated in the dropdown and then get the description value back.
Well, this has nothing to do with Telerik so far. My problem was when in my datacontract I was using nullable
and wanted to use filter in my RadGrid. It would not work. I debugged my could and
found that the FilterExpression was set to "([Corporate] = INDUSTRY)". Now, this is just plain wrong.
It should be "([Corporate] = 'INDUSTRY')". Note the single quotation marks.

I fixed this issue by adding DataType="System.String" and then the filtering looked ok, but nope
it still did not work.

Why did it not work?

Well, it turned out that when I was using

[DataMember]
public Corporate Corporate { get; set; }

in my contract it works, but when using a nullable property

[DataMember]
public Corporate? Corporate { get; set; }

it did not work.

I fixed it by adding another class called searchcriteria and use that one
for my initial search containing my nullable Corporate and then my original class without nullable
for the filtering. This is very irritating. Can someone at Telerik look into this matter.

Best regards,
Claes


2 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 16 Feb 2011, 09:23 AM
Hello Claes,

I am afraid filtering on an EnumType column is not a supported scenario. When the RadGrid is bound to the Enum types it will get the values corresponding to the enum option. For example:  0, 1, 2 ... To achieve the desired functionality you need to set the DataType="System.String" to the column and bound the RadGrid to the string representations of the enum options . In this way the RadGrid will convert the enum options to strings (in your case COMPANY, INDUSTRY.. ) and you could filter them.

Additionally I am sending you a simple example which demonstrates this.

Greetings,
Radoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ross Presser
Top achievements
Rank 1
answered on 06 Jul 2012, 02:53 PM
Here is a fairly easy way to achieve the same effect without a table as datasource. Assume assetItems is a protected property that returns a list of assetItem, the real datasource, and UsageType is the enum field in assetItem we need to turn to string:

    Protected Sub rGrid_NeedDataSource(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgsHandles rGrid.NeedDataSource
        rGrid.DataSource = (From x In assetItems
                            Select New assetItemX(x)).ToList()
    End Sub

    <Serializable()> _
    Protected Class assetItemX
        Inherits aleyant.w2pPlatform.AssetItem
        Public Sub New(x As aleyant.w2pPlatform.AssetItem)
            AssetID = x.AssetID
            AssetType = x.AssetType
            Name = x.Name
' (omitted rest of fields)
        End Sub
        Public ReadOnly Property UsageTypeString As String
            Get
                Return UsageType.ToString()
            End Get
        End Property
    End Class

Now you can bind the radgrid column to UsageTypeString instead of UsageType.

ETA: the AssetX class must be marked serializable or the page will complain sometimes.
Tags
Grid
Asked by
Claes
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Ross Presser
Top achievements
Rank 1
Share this question
or