Sugu Selvam
Sugu Selvam

Reputation: 64

how to bind the enum types of dropdown to the Dropdown list?

public enum CandidateStatus : int {

    None = 0,

    Invalid = 1,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Pending Approval")]
    PendingApproval = 2,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Open")]
    Open = 4,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Screening")]
    Screening = 8,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Interviewing")]
    Interviewing = 16,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Offers Issued")]
    OffersIssued = 32,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Hired")]
    Hired = 64,

    [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="Cancelled")]
    Cancelled = 128,
}

this is my enum code in linq.cs file.

Here CandidateStatus is the choice column which is defined in Sharepoint list. how can i bind this choice field of "CandidateStatus" in dropdown list and how can i insert this selected dropdown value into the Sharepoint list? Can you anyone help on this please......

Upvotes: 1

Views: 982

Answers (1)

onof
onof

Reputation: 17367

I think your only option is to map the enum values to an object:

myDropDown.TextField = "Value";
myDropDown.ValueField = "ID";

myDropDown.DataSource = ((CandidateStatus []) Enum.GetValues(typeof(CandidateStatus))
   .Select(c => new {
                      ID = (int)c, 
                      Value = ( typeof(CandidateStatus)
                       .GetField(c.ToString())
                       .GetCustomAttributes(
                         typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false) 
                           as EnumStringValueAttribute[]).FirstOrDefault() 
                        ?? c.ToString()
                    });
myDropDown.DataBind();

Upvotes: 2

Related Questions