Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:12+00:00 2026-05-26T16:01:12+00:00

I have a DataGridView which allows the user to add a new object to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:01:13+00:00Added an answer on May 26, 2026 at 4:01 pm

    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"
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView in which one column has data that the user needs
I have a DataGridView object to which I've bound a list of objects (of
I have a DataGridView which I add data to programatically. I have the AutoSizeRowsMode
I have a datagridview which we will call dataGridViewExample. My object (the uncommon datatypes
I have a DataGridView which is filling from Table in SQL Server Database. One
I have a datagridview in which one column contains a custom class, which I
I have a datagridview where the users can select which subset of columns to
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
So I have a DataGridView which is used to display a list of custom
I have a DataGridView in one of my form which at a certain point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.