0

I need to get all items from SPChoiceField and insert in dropDownList ?

This is my code to get from everyone ChoiceField, I need to get only from SPChoiceField with name "Company"

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList wayList = web.Lists["WayBill"];

                        foreach (SPField field in wayList.Fields)
                        {
                            //check whether field type is multi choice or not
                            if (field.Type == SPFieldType.Choice)
                            {
                                SPFieldChoice choice = (SPFieldChoice)field;
                                foreach (string choiceName in choice.Choices)
                                {
                                    ddCompany.Items.Add(choiceName);
                                }
                            }
                        }
                    }
                }

How to do this ?

1

This is the solution (:

SPList wayList = web.Lists["WayBill"];

     SPFieldChoice dropdown = wayList.Fields.GetFieldByInternalName("Company") as SPFieldChoice;

     foreach (string choiceName in dropdown .Choices)
      {
         ddCompany.Items.Add(choiceName);
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.