Bind Multi Choice Field Data To Drop Down List

Many times, we come across the situation where we need to bind the value of multi choice field data to drop down list. The post will help to get and bind the data to dropdowns.

Scenario

We have two fields in SharePoint custom list, where one is Title and another is Browser version name.

Whenever we select one title, it will bind its related Browser versions, which are based on the Title selection.

Steps
  1. Create custom list Browsers, add column Supported Browsers and Browser version is having type choice and add the values in the list
  2. Create SharePoint, empty project -> add Visual Web part.
  3. Open .ascx file and add the code given below.
    1. <style>  
    2.     .main {  
    3.         display: inline;  
    4.         width: 100%;  
    5.         padding: 10px;  
    6.     }  
    7.   
    8.     .left {  
    9.         width: 50%;  
    10.         float: left;  
    11.     }  
    12.   
    13.     .right {  
    14.         width: 50%;  
    15.         float: right;  
    16.     }  
    17. </style>  
    18. <div class="main">  
    19.     <div>  
    20.         <div class="left">  
    21.             <asp:Label ID="lblBIVersion" runat="server" Text="BI Version"></asp:Label>  
    22.         </div>  
    23.         <div class="right">  
    24.             <asp:DropDownList ID="ddlversion" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlversion_SelectedIndexChanged"></asp:DropDownList>  
    25.         </div>  
    26.     </div>  
    27.     <div>  
    28.         <div class="left">  
    29.             <asp:Label ID="lblBrowsers" runat="server" Text="Supported Browsers"></asp:Label>  
    30.         </div>  
    31.         <div class="right">  
    32.             <asp:DropDownList ID="ddlBrowsers" runat="server"></asp:DropDownList>  
    33.         </div>  
    34.     </div>  
    35. </div>  
  4. open .ascx.cs file and add the code and functions given below. For this, we need to add refernce of using Microsoft.SharePoint.
    1. protected void Page_Load(object sender, EventArgs e) {  
    2.     try {  
    3.         using(SPSite site = new SPSite("server url")) {  
    4.             using(SPWeb web = site.OpenWeb()) {  
    5.                 if (!Page.IsPostBack) {  
    6.                     BindBIVersion(web);  
    7.                     BindData(web, "");  
    8.                 }  
    9.             }  
    10.         }  
    11.     } catch (Exception) {}  
    12. }  
    13. public void BindBIVersion(SPWeb oweb) {  
    14.     SPList list = oweb.Lists["PCM"];  
    15.     ddlversion.DataSource = list.Items;  
    16.     ddlversion.DataValueField = "Title";  
    17.     ddlversion.DataTextField = "Title";  
    18.     ddlversion.DataBind();  
    19. }  
    20. private void BindData(SPWeb oweb, string selectedBI) {  
    21.     try {  
    22.         DataTable dt1 = new DataTable();  
    23.         SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue();  
    24.         SPQuery oQuery = new SPQuery();  
    25.         SPList list = oweb.Lists.TryGetList("listname");  
    26.         oQuery.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + selectedBI + "</Value></Eq></Where>";  
    27.         SPListItemCollection collListItems = list.GetItems(oQuery);  
    28.         dt1.Columns.Add("BrowserName"typeof(string));  
    29.         dt1.Columns.Add("BrowserValue"typeof(string));  
    30.         foreach(SPListItem item in collListItems) {  
    31.             choices = new SPFieldMultiChoiceValue(item["Supported Browsers"].ToString());  
    32.         }  
    33.         for (int i = 0; i < choices.Count; i++) {  
    34.             dt1.Rows.Add(choices[i], choices[i]);  
    35.         }  
    36.         ddlBrowsers.Items.Clear();  
    37.         ddlBrowsers.DataSource = dt1;  
    38.         ddlBrowsers.DataTextField = "BrowserName";  
    39.         ddlBrowsers.DataValueField = "BrowserValue";  
    40.         ddlBrowsers.DataBind();  
    41.     } catch (Exception) {}  
    42. }  
    43. protected void ddlversion_SelectedIndexChanged(object sender, EventArgs e) {  
    44.     string selectedValue = ddlversion.SelectedValue;  
    45.     using(SPSite site = new SPSite("server url")) {  
    46.         using(SPWeb web = site.OpenWeb()) {  
    47.             BindData(web, selectedValue);  
    48.         }  
    49.     }  
    50. }  
  5. Build the project and check for the errors.
  6. Deploy the project. 
  7. Create Web part page and the Web part given above. You will find on selection of Title (BI version) and second drop downs options gets changed.
The main thing is that the Browser version is having choice field and is based on title selection; we are fetching data from the same list.

Both the drop downs are bind dynamically.

Thanks in advance.