Set the selected item in code

5 posts, 0 answers
  1. Chris Kirkman
    Chris Kirkman avatar
    101 posts
    Member since:
    Apr 2010

    Posted 22 Jan 2018 Link to this post

    Once setting the .DataSource of a dropdownlist to a view model.  How can I immediately tell the dropdownlist to select a specific value within the model?

    // UI

                ReferenceProjectDropDownList.DisplayMember = "Name";
                ReferenceProjectDropDownList.DataSource = new ProjectsViewModel(snc.Measuring.Projects.ToList()).Projects;
                if (null != project)
                {
                    ReferenceProjectDropDownList.SelectedItem = ???                   
                }

     

    // view model

    public class ProjectsViewModel
        {
            public ProjectsViewModel(List<ScanProject> projects)
            {
                Projects = new List<ProjectViewModel>();
                projects.ForEach(p =>
                {
                    Projects.Add(new ProjectViewModel()
                    {
                        Id = p.Id,
                        Name = p.Name,
                    });
                });
            }
            public List<ProjectViewModel> Projects;
        }
        public class ProjectViewModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

  2. Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 23 Jan 2018 Link to this post

    Hi Chris,

    This works fine on my side. I have attached my test project. Could you please check it and let me know how it differs from your real setup? 

    If the DropDownList is not added to a form you need to call the following methods to load the data and the layout:
    radDropDownList1.LoadElementTree();
    Application.DoEvents();

    I am looking forward to your reply.
     
    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Chris Kirkman
    Chris Kirkman avatar
    101 posts
    Member since:
    Apr 2010

    Posted 23 Jan 2018 in reply to Dimitar Link to this post

    I think I should have been more clear.  I don't want to grab an item by index.  I need to use something similar to LINQ where I can find the item to bind to via something like the following...

    ReferenceProjectDropDownList.SelectedItem = ReferenceProjectDropDownList.Items.Select(t =>
           (t.DataBoundItem as ProjectViewModel).Id == project.Id);
  4. Chris Kirkman
    Chris Kirkman avatar
    101 posts
    Member since:
    Apr 2010

    Posted 23 Jan 2018 Link to this post

    This isn't what I wanted to do, but it works.  I'd still prefer a one line LINQ statement to get the item, perhaps using something like .Find();

    ReferenceProjectDropDownList.SelectedItem = ReferenceProjectDropDownList.
                        Items[GetProject(ReferenceProjectDropDownList, project.Id)];
     
    int GetProject(CommandBarDropDownList list, int id)
            {
                int result= 0;
                foreach (RadListDataItem item in list.Items)
                {
                    if ((item.DataBoundItem as ProjectViewModel).Id == id)
                    {
                        result = item.Index;
                    }
                }
     
                return result;
            }
  5. Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 24 Jan 2018 Link to this post

    Hi Chris,

    You way is good. If you specify the ValueMember you can use a more direct approach:
    radDropDownList1.DisplayMember = "Name";
    radDropDownList1.ValueMember = "Id";
    radDropDownList1.DataSource = new ProjectsViewModel(new List<string> { "item1", "item2", "Item3", "Item4" }).Projects;
      
    radDropDownList1.SelectedItem = radDropDownList1.Items.Where(x => (int)x.Value == 3).FirstOrDefault();
    //or
    radDropDownList1.SelectedValue = 2;

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top