Link to home
Start Free TrialLog in
Avatar of Arikael
ArikaelFlag for Switzerland

asked on

paint a userdefined datagridviewcolumn

Hi experts

I created a datagridviewcolumn which contains a listview. But I'm struggling with displaying the whole thing.
As I understand it, every cell shares the same listview, so I can't just display the listview, right?

I think I need the Paint event of my custom cell to draw my content but I have no idea how.

thanks
public class DataGridViewListViewColumn : DataGridViewColumn
    {
        public DataGridViewListViewColumn()
            : base(new DataGridViewListViewCell())
        {
            
        }

public class DataGridViewListViewCell : DataGridViewTextBoxCell
    {
        public DataGridViewListViewCell()
            : base()
        {

        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            DataGridViewListView ctl = new DataGridViewListView();
            ctl.Show();
        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, "", errorText, cellStyle, advancedBorderStyle, paintParts);
        }

        public override Type EditType
        {
            get
            {
                return typeof(DataGridViewListView);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(Int32);
            }
            set
            {
                base.ValueType = value;
            }
        }
    }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if(value != null &&
                    !value.GetType().IsAssignableFrom(typeof(DataGridViewListViewCell)))
                {
                    throw new InvalidCastException("Must be a CalendarCell");
                }

                base.CellTemplate = value;
            }
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arikael

ASKER

please note that you have to change somethings in the code (for example changing MyRow to DataGridViewRow)