0

I have extended DataGridView. I want to add the new control to the toolbox without adding a reference to a different assembly (using the right-click option "Choose Items"). The control is in the same project of the form, and I do not want to separate them. How can I achieve this?

Thanks.

Edit: This could not possibly a duplicate of a question about user controls if it's not a user control.

Edit 2: The code itself (it's a work in process. It's not finished):

class BindedDataGrid<T> : DataGridView
{
    public BindedDataGrid()
    {
        InitializeComponent();

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute)));

        foreach (PropertyInfo property in properties)
        {
            var column = new DataGridViewColumn()
            {
                HeaderText = ((property.GetCustomAttributes(true)[0]) as BindingValueAttribute).Value
            };

            Columns.Add(column);
        }
    }
}
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82

1 Answers1

0
public class BindedDataGrid : DataGridView
    {
        public BindedDataGrid()
        {
        }

        public BindedDataGrid(Type bindType)
        {
            IEnumerable<PropertyInfo> properties = bindType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute)));

            foreach (PropertyInfo property in properties)
            {
                var prop = property.GetCustomAttributes(true)[0];
                if (prop is BindingValueAttribute)
                {
                    var column = new DataGridViewColumn()
                    {
                        HeaderText = property.Name
                    };
                    column.CellTemplate = new DataGridViewTextBoxCell();
                    Columns.Add(column);
                }
            }
        }
    }

    public class BindingValueAttribute  : Attribute
    {
        public string Value { get; set; }
    }

    public class BindOne
    {
        [BindingValueAttribute()]
        public string Name { get; set; }

        [BindingValueAttribute()]
        public int Age { get; set; }
    }
  1. first of all make BindedDataGrid none generic
  2. create a non parameterized constructor

        private void InitializeComponent()
    {
        this.bindedDataGrid1 = new PlayingWithThread.BindedDataGrid(typeof(BindOne));
        ((System.ComponentModel.ISupportInitialize)(this.bindedDataGrid1)).BeginInit();
    
Z.R.T.
  • 1,543
  • 12
  • 15