How C # Displays strings and icons in the DataGridView Rowheader

Source: Internet
Author: User

"Data collection from the network"


In the DataGridView cellpainting event, add the following code

private void Datagridview1_cellpainting (object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.rowindex >= 0 && E.columnindex = = 1)

{

Rectangle newrect = new Rectangle (e.cellbounds.x, E.cellbounds.y, E.cellbounds.height, e.cellbounds.height);

Create a new icon

System.Drawing.Icon ico = new Icon ("F://xx.ico");

using (Brush Gridbrush = new SolidBrush (This.dataGridView1.GridColor),

BackColorBrush = new SolidBrush (E.cellstyle.backcolor))

{

using (pen Gridlinepen = new Pen (gridbrush,2))

{

Erase the cell.

E.graphics.fillrectangle (BackColorBrush, e.cellbounds);

Crossed

Point P1 = new Point (E.cellbounds.left+e.cellbounds.width, e.cellbounds.top);

Point P2 = new Point (E.cellbounds.left+e.cellbounds.width, e.cellbounds.top+e.cellbounds.height);

Point p3 = new Point (e.cellbounds.left,e.cellbounds.top+ e.cellbounds.height);

Point[] ps = new POINT[]{P1,P2,P3};

E.graphics.drawlines (GRIDLINEPEN,PS);

Draw icon

E.graphics.drawicon (ico, newrect);

Draw a String

E.graphics.drawstring ("123", E.cellstyle.font, Brushes.crimson, E.cellbounds.left +, E.cellbounds.top, Stringformat.genericdefault);

E.handled = true;

}

}

}

}

Exceptions have other methods for reference, also in the DataGridView cellpainting event

"A" is displayed on the 1 (ie rowheader) column

if (e.rowindex >= 0 && e.columnindex = 0)

{

E.graphics.drawstring ("A", This.dataGridView1.Font, brushes.red, E.cellbounds.top + 5);

}

"A" is covered by the 1 (ie rowheader) column.

if (e.rowindex >= 0 && E.columnindex = = 1)

{

E.graphics.drawstring ("A", This.dataGridView1.Font, brushes.red, E.cellbounds.top + 5);

}

XX picture on-1 (ie rowheader) column

if (e.rowindex >= 0 && e.columnindex = 0)

{

Image img = image.fromfile (@ "f:/xx.bmp");

E.graphics.drawimage (IMG, 0, e.cellbounds.top);


}

XX picture is covered by-1 (ie rowheader) column

if (e.rowindex >= 0 && E.columnindex = = 1)

{

Image img = image.fromfile (@ "f:/xx.bmp");

E.graphics.drawimage (IMG, 0, e.cellbounds.top);

}

XX picture is covered by-1 (ie rowheader) column

if (e.rowindex >= 0 && E.columnindex = = 1)

{

Image img = image.fromfile (@ "f:/xx.bmp");

E.graphics.drawimage (IMG, e.cellbounds.left, e.cellbounds.top);

}

XX picture is covered by 0 columns

if (e.rowindex >= 0 && e.columnindex = 0)

{

Image img = image.fromfile (@ "f:/xx.bmp");

E.graphics.drawimage (IMG, e.cellbounds.left, e.cellbounds.top);

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.