This is a migrated thread and some comments may be shown as answers.

Set the selected item in code

4 Answers 106 Views
This is a migrated thread and some comments may be shown as answers.
Chris Kirkman
Top achievements
Rank 1
Chris Kirkman asked on 22 Jan 2018, 04:51 PM

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; }
    }

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 23 Jan 2018, 09:27 AM
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.
0
Chris Kirkman
Top achievements
Rank 1
answered on 23 Jan 2018, 02:16 PM

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);
0
Chris Kirkman
Top achievements
Rank 1
answered on 23 Jan 2018, 02:28 PM

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;
        }
0
Dimitar
Telerik team
answered on 24 Jan 2018, 09:48 AM
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.
Asked by
Chris Kirkman
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Chris Kirkman
Top achievements
Rank 1
Share this question
or