Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
455
WinGrid: Conditionally formatting a enum value
posted

Hi,

I have WinGrid bound to list and one of the column of list is a enum OptionType whose values are None, American and European. I want to display the value of this column only when the OptionType is American or European (otherwise I want to show blank cell).

I wrote the following code in the InitializeRow event

MyDataItem  item  = e.Row.ListObject as MyDataItem ;

if (item != null){

if (item.OptionType == OptionType.None)

e.Row.Cells["OptionType"].Value = null;

else

e.Row.Cells["OptionType"].Value = item.OptionType;

}

 

But the above code is giving error "Data Error" stating can not convert "null" into OptionType, how this can be achieved?

 

  • 469350
    Verified Answer
    Offline posted

    The code you have here will not work. What you are doing is attemping to change the actual value of the cell, not just the display. Since the DataType of your column is an enum type, it cannot accept a null value. And I don't think you would want it to, anyway, since this would actually change your data.

    If you just want to change the display, then I recommend using a ValueList. You could create a ValueList in code, in the InitializeLayout event of the grid, for example. Populate the ValueList with 3 items with DataValue and DisplayText like so:

    valueList.Items.Add(OptionType.None, String.Empty);

    valueList.Items.Add(OptionType.American, "American");

    valueList.Items.Add(OptionType.European, "European");

    Then set the ValueList property on the column to this ValueList. This not only translates the value into the desired display text, it also gives the user a dropdown list from which that can choose an option.