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;
CntrImage.MouseClick += new MouseEventHandler(MouseClick);
}
private void MouseClick(object sender, MouseEventArgs e)
{
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);
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);