Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to implement custom datagridview header but it is not working properly.
it is not drawing on grid

Please help

What I have tried:

my custom header class

public class ImageTextHeaderCell : DataGridViewColumnHeaderCell
    {
        public readonly Label lblText;
        public readonly PictureBox CntrImage;


        public event EventHandler ImageClick;

        public ImageTextHeaderCell()
        {
            lblText = new Label();
            CntrImage = new PictureBox();
            lblText.Font = Control.DefaultFont;

            

            //lblText.MouseClick += new MouseEventHandler(MouseClick);
            CntrImage.MouseClick += new MouseEventHandler(MouseClick);
            
        }

        private void MouseClick(object sender, MouseEventArgs e)
        {
            //Debug.WriteLine("x:{0}, y:{1}, button:{2}", e.X, e.Y, e.Button);
            //OnClick(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));
            if (ImageClick != null)
            {
                ImageClick(this, EventArgs.Empty);
            }


        }

        public string Text
        {
            get { return lblText.Text; }
            set { lblText.Text = value; }
        }

        public Image ControlImage
        {
            get { return CntrImage.Image; }
            set { CntrImage.Image = value; }
        }

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

            lblText.Location = new Point(cellBounds.Left, cellBounds.Top + 3);
            lblText.Height = cellBounds.Height;
            lblText.Width = cellBounds.Width - 41;

            CntrImage.Location = new Point(lblText.Right + 1, cellBounds.Top + 3);
            lblText.Height = cellBounds.Height;
            
        }
        

    }


and my code on form

DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            dataGridViewTextBoxColumn.DefaultHeaderCellType = typeof(ImageTextHeaderCell);
            //dataGridViewTextBoxColumn.HeaderText = "Password";
            ImageTextHeaderCell dataGridViewImageColumnHeaderCell = dataGridViewTextBoxColumn.HeaderCell as ImageTextHeaderCell;
            dataGridViewImageColumnHeaderCell.Text = "hello";
            dataGridViewImageColumnHeaderCell.ControlImage = global::WFAppGrid.Properties.Resources.Star;
            dataGridViewImageColumnHeaderCell.ImageClick += dataGridViewImageColumnHeaderCell_ImageClick;
            
            this.dataGridView1.Columns.Add(dataGridViewTextBoxColumn);
Posted
Comments
RickZeeland 7-Mar-17 14:11pm    
Maybe using an ImageList could help: http://www.viblend.com/forum/d_postsm57_Display-an-image-in-a-Column-Header.aspx#post57

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900