• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

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

0
Arikael
Asked:
Arikael
1 Solution
 
Roshan DavisCommented:
to paint a cell this link will help
http://stackoverflow.com/questions/177133/overriding-datagridviewtextboxcell-paint-method
pasting the part which I am referring
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {

            DataGridViewPaintParts pp = DataGridViewPaintParts.Background | DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground
                | DataGridViewPaintParts.ErrorIcon;

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pp);

            string text = formattedValue as string; //<- HERE YOU NEED TO GIVE WHAT YOU WANT TO DISPLAY

            int indent = ((MyRow)OwningRow).Indent * INDENTCOEFFICIENT;
            strFmt.Trimming = StringTrimming.EllipsisCharacter;
            Rectangle r = cellBounds;
            r.Offset(indent, 0);
            r.Inflate(-indent, 0);
            graphics.DrawString(text, cellStyle.Font, Brushes.Black, r, strFmt);
        }

Open in new window

0
 
ArikaelAuthor Commented:
please note that you have to change somethings in the code (for example changing MyRow to DataGridViewRow)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now