How to display an image in a datagridview column header?

前端 未结 4 563
傲寒
傲寒 2020-12-03 15:40

At run-time, I am adding a DataGridView to a windows form. The final column is a DataGridViewImageColumn:

Dim InfoIconColumn As New         


        
4条回答
  •  青春惊慌失措
    2020-12-03 16:21

    One way you can do this is to use the CellsPainting event to draw the bitmap for a particular header cell. Here is code that does this assuming the bitmap is in an imagelist.

    //this.images is an ImageList with your bitmaps
    void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex == 1 && e.RowIndex == -1)
        {
            e.PaintBackground(e.ClipBounds, false);
    
            Point pt = e.CellBounds.Location;  // where you want the bitmap in the cell
    
            int offset = (e.CellBounds.Width - this.images.ImageSize.Width) / 2;
            pt.X += offset;
            pt.Y += 1;
            this.images.Draw(e.Graphics, pt, 0);
            e.Handled = true;
        }
    }
    

提交回复
热议问题