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 8044619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:17:10+00:00 2026-06-05T05:17:10+00:00

I am using EF to return a List of Processes, with one to many

  • 0

I am using EF to return a List of Processes, with one to many flags. Flags are unique, they may increase or decrease depending on requirements. The data structure roughly translates to:

public enum FlagTypes
{
    OnlyOnWeekends,
    OnlyOnHolidays
}

public class Process
{
    public DateTime Date { get; set; }
    public String Description { get; set; }
    public Dictionary<FlagTypes, Flag> Flags { get; set; }
}

public class Flag
{
    public FlagTypes Type { get; set; }
    public bool Enabled { get; set; }
}

I would like to display this in a DataGridView like so:

Date | Description | OnlyOnWeekends | OnlyOnHolidays [|... more flags as needed]

.. while also making it editable.

I was able to work around the limits of DataGridView to display the table using a custom column and cell

public class EnumerationColumn : DataGridViewColumn
{
    public FlagTypes EnumerationType { get; set; }

    public EnumerationColumn(FlagTypes enumerationType)
        : base(new EnumerationCell())
    {
        EnumerationType = enumerationType;
    }
    
    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a EnumerationCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(EnumerationCell)))
            {
                throw new InvalidCastException("Must be a EnumerationCell");
            }
            base.CellTemplate = value;
        }
    }
    
    public class EnumerationCell : DataGridViewCheckBoxCell
    {
        private EnumerationColumn Parent
        {
            get
            {
                var parent = base.OwningColumn as EnumerationColumn;
                if(parent == null)
                {
                    throw new NullReferenceException("EnumerationCell must belong to a EnumerationColumn");
                }
                return parent;
            }
        }
        
        private Dictionary<FlagTypes, Flag> GetFlags(int rowIndex)
        {
            var flags = base.GetValue(rowIndex) as Dictionary<FlagTypes, Flag>;
            return flags ?? new Dictionary<FlagTypes, Flag>();
        }
        
        protected override object GetValue(int rowIndex)
        {
            var flags = GetFlags(rowIndex);
            if (flags.ContainsKey(Parent.EnumerationType))
            {
                return flags[Parent.EnumerationType].Enabled;
            }
            return false;
        }
    }
}

And creating the columns

grid.AutoGenerateColumns = false;
grid.DataSource = processes; // List<Process>

var dateCol = new CalendarWidgetColumn();
dateCol.DataPropertyName = "Date";
dateCol.HeaderText = "Date";

var descCol = new DataGridViewTextBoxColumn();
descCol.DataPropertyName = "Description";
descCol.HeaderText = "Description";

grid.Columns.Add(dateCol);
grid.Columns.Add(descCol);
        
foreach(string name in Enum.GetNames(typeof(FlagTypes)))
{
    FlagTypes flag;
    if(FlagTypes.TryParse(name, out flag))
    {
        var enumCol = new EnumerationColumn(flag);
        enumCol.DataPropertyName = "Flags";
        enumCol.HeaderText = String.Format("{0}?", name);
        grid.Columns.Add(enumCol);
    }
}

I cannot figure out how to intercept the call to save to the DataSource, so it is throwing an Exception trying to set a bool (the checkbox value) to a Dictionary (the Flags DataProperty). I have looked at the CellValuePushed event, but that fires after the fact. Any ideas?

Or perhaps an easier way to approach this all together?

(Eventually I want to wrap the processes list with a BindingList so I can also create new rows directly from the DataGridView)


Solution (as suggested by @Erez Robinson below)

Step 1: Modify DataStructure to allow easy access to underlying dictionary

public enum FlagType
{
    OnlyOnWeekends,
    OnlyOnHolidays
}

public class Process
{
    public DateTime Date { get; set; }
    public String Description { get; set; }
    public Dictionary<FlagType, Flag> Flags { get; set; }

    public bool this[FlagType flagType]
    {
        get
        {
            if(!Flags.ContainsKey(flagType))
            {
                Flags.Add(flagType, new Flag(flagType, false));
            }
            return Flags[flagType].Enabled;
        }
        set
        {
            Flags[flagType].Enabled = value;
        }
    }
}

public class Flag
{
    public FlagType Type { get; set; }
    public bool Enabled { get; set; }

    public Flag(FlagType flagType, bool enabled)
    {
        Type = flagType;
        Enabled = enabled;
    }
}

Step 2: Create a container that derives from ITypedList

class ProcessCollection : List<Process>, ITypedList
{
    protected IProcessViewBuilder _viewBuilder;

    public ProcessCollection(IProcessViewBuilder viewBuilder)
    {
        _viewBuilder = viewBuilder;
    }

    #region ITypedList Members

    protected PropertyDescriptorCollection _props;

    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        if (_props == null)
        {
            _props = _viewBuilder.GetView();
        }
        return _props;
    }

    public string GetListName(PropertyDescriptor[] listAccessors)
    {
        return ""; // was used by 1.1 datagrid
    }

    #endregion
}

Step 3: Move dynamic property creation to a ViewBuilder

public interface IProcessViewBuilder
{
    PropertyDescriptorCollection GetView();
}

public class ProcessFlagView : IProcessViewBuilder
{
    public PropertyDescriptorCollection GetView()
    {
        List<PropertyDescriptor> props = new List<PropertyDescriptor>();
        
        props.Add(new DynamicPropertyDescriptor(
            typeof(Process),
            "Date",
            typeof(DateTime),
            delegate(object p)
            {
                return ((Process)p).Date;
            },
            delegate(object p, object newPropVal)
                {
                    ((Process)p).Date = (DateTime)newPropVal;
                }
        ));

        props.Add(new DynamicPropertyDescriptor(
            typeof(Process),
            "Description",
            typeof(string),
            delegate(object p)
            {
                return ((Process)p).Description;
            },
            delegate(object p, object newPropVal)
                {
                    ((Process) p).Description = (string) newPropVal;
                }
        ));

        foreach (string name in Enum.GetNames(typeof(FlagType)))
        {
            FlagType flag;
            if (FlagType.TryParse(name, out flag))
            {

                props.Add(new DynamicPropertyDescriptor(
                              typeof (Process),
                              name,
                              typeof (bool),
                              delegate(object p)
                                  {
                                      return ((Process) p)[flag];
                                  },
                              delegate(object p, object newPropVal)
                                  {
                                      ((Process) p)[flag] = (bool) newPropVal;
                                  }
                              ));
            }
        }

        PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];
        props.CopyTo(propArray);
        
        return new PropertyDescriptorCollection(propArray);
    }
}

Step 4: Bind the collection to the DataGridView

ProcessCollection processes = new ProcessCollection(new ProcessFlagView());

// Add some dummy data ...
processes.Add( ... );

// If you want a custom DataGridViewColumn, bind it before you bind the DataSource
var dateCol = new CalendarColumn();
dateCol.DataPropertyName = "Date";
dateCol.HeaderText = "Date";
grid.Columns.Add(dateCol);

grid.DataSource = processes;
  • 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-06-05T05:17:13+00:00Added an answer on June 5, 2026 at 5:17 am

    You can derive from DataGridView and shadow the DataSource property with the new keyword.

    But I think that you need to change your concept.
    I would not touch the datagridview.

    Exposes these Flags as seperate Properties. Add these classes to a binding list.
    You can dynamicaly create properties using reflection.

    Please read this article, it will point you in the right direction: ITypedList

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario I have a method that returns a list of processes using WMI. If
I'm using the twitter API to return a list of status updates and the
I am using ObjectContext.ExecuteStoreQuery to return a list of entities, about 30K. I need
I'm using LINQtoSQL and I want to return a list of matching records for
I'm using the Dynamic LINQ library code sample to dynamically return some data. The
In a class that processes a list of images over HTTP, one image throws
I'm using a web service which returns a list of products. I created a
I need to display the content of another page from a function using return.
In ASP.NET MVC3, when a view model is passed into a view using return
I am using the return from the following call as the datasource for a

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.