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?
Ok, I made some changes. Now it works fine at first. Change Resource Class to:
Create new class for the Grid combo box:
Sample usage:
and