Steve Rukuts
Steve Rukuts

Reputation: 9367

DataGridViewComboBoxColumn does not allow me to select an object from a list

I have a DataGridView which allows the user to add a new object to a list. One of the important parts of this is to select the type from a user-definable list.

I am defining the columns like so:

this.DataGridView.Columns.Add(new DataGridViewComboBoxColumn
                              {
                                  Name = "Resource",
                                  DataPropertyName = "Resource",
                                  DataSource = new BindingSource { DataSource = this.Document.Resources },
                                  ValueType = typeof(Resource),
                                  DisplayMember = "Name"
                              });

I then set the DataSource of the DataGridView to the list of UserResources:

BindingList<UserResource> relatedResources = new BindingList<UserResource>(this.User.ResourcesRequired);
this.DataGridView.DataSource = relatedResources;

The Resource class layout looks like this:

public class Resource
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int InitialLevel { get; set; }
}

The UserResource class looks like this:

public class UserResource
{
    public Resource Resource { get; set; }
    public int CurrentLevel { get; set;
}

The User class looks like this:

public class User
{
    public string Name { get; set; }
    public IEnumerable<UserResource> Resources { get; set; }
}

I can see a list of available Resource types, but the item that is selected in the DataGridViewComboBoxCell does not stay selected. After selecting the item, when I move on to the next field, the DataGridViewComboBoxCell clears itself.

Other fields in the row will be written to my new UserResource instance, but the Resource reference will not be saved, and the property remains null on the new UserResource instance.

In case people are wondering, I am not using any sort of object relational mapper or any sort of database layer at all. All objects in memory are written to and read from an XML document.

I'm honestly unsure about how to continue debugging this issue from here. Does anybody have any recommendations?

Upvotes: 0

Views: 1693

Answers (3)

Leblanc Meneses
Leblanc Meneses

Reputation: 3091

                    <telerik:DataFormComboBoxField Grid.Row="0" Grid.Column="2" Label="Time Zone:" LabelPosition="Above">
                    <telerik:RadComboBox SelectedValue="{Binding TimeZoneID, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                        ItemTemplate="{StaticResource DataTemplateTimeZone}" 
                                        SelectedValuePath="ID" 
                                        ItemsSource="{Binding DataSource.TimeZoneDataSource, Source={StaticResource vmProxy}}"
                                        IsEnabled="True"/>
                </telerik:DataFormComboBoxField>

I would recommend using a viewmodel that contains an observablecollection and using http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

as i do with StaticResource vmProxy

Upvotes: 0

DeveloperX
DeveloperX

Reputation: 4683

Ok, I made some changes. Now it works fine at first. Change Resource Class to:

    public class Resource
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public int InitialLevel { get; set; }
//added by dx
        public new string ToString()
        {
            return Name;
        }
    }

Create new class for the Grid combo box:

    public class  ComboSource
    {
        public string Name
        {
            get
            {
                if (SourceValue != null)
                    return SourceValue.ToString();
                return string.Empty;
            }
        }
        public Resource SourceValue{ get; set; }

    }

Sample usage:

       private List<ComboSource> resources = new List<ComboSource>();
            this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs1", Description = "a"} });
            this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs2", Description = "b" } });
            this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs3", Description = "c" } });

and

        this.dataGridView1.Columns.Add(new DataGridViewComboBoxColumn
        {
            Name = "Resource",
            DataPropertyName = "Resource",
            ValueMember = "SourceValue",
            DataSource = new BindingSource { DataSource = this.resources },
            ValueType = typeof(Resource),
            DisplayMember = "Name"
        });

Upvotes: 2

Hogan
Hogan

Reputation: 70513

I don't think the problem is with this control. You can select the item just fine. As you say when you move on the form the item is cleared.

This means there is other source which is doing validation on this control and deciding to clear it. Please look for other parts of your code or hooks to the change event which might be modifying the selected value.

I'd be willing to help more but I'd need to see the full code, my email address is on my profile if you are willing to share.

Upvotes: 1

Related Questions