Wednesday, 27 November 2013

Bind Comma Separated string Values into Dropdown

 string str = "A,B,C,D,";
            //Declare a arraylist for getting comma separated string
            ArrayList arr = new ArrayList();
            //check wether the re is comma in the end of the string
            if (str.Trim().EndsWith(","))
            {
                str = str.Substring(0, str.Length - 1);
            }
            //split the comma separated string into arraylist
            arr.AddRange(str.Split(','));
            //loop through the arraylist items & add the item to Dropdownlist
            for (int i = 0; i < arr.Count; i++)
            {
                DropDownList1.Items.Insert(i, new ListItem(arr[i].ToString(), (i + 1).ToString()));
            }

No comments:

Post a Comment