简介
这篇文章有助于加载在一个DataGridView的行标题单元格的形象。根据我的调查,发现,我们不能直接装入一个行标题单元格(可以是假的,虽然)的图像。但我想通了一个行标题单元格中的图像,通过图像转换成一个图标的方式来加载??然后显示它使用的事件dataGridView1_RowPostPaint(我不知道这是否是最好的方式)。{S0}
我添加了"RedCross??在我的项目的ImageList的形象。如果我们尝试较大程度上调整图像的大小,图像质量会变形。尝试加载在图像列表的确切大小的图像,我们更愿意显示行标题单元格。private void dataGridView1_RowPostPaint
(object sender, DataGridViewRowPostPaintEventArgs e)
{
//Convert the image to icon, in order to load it in the row header column
Bitmap myBitmap = new Bitmap(imageList1.Images[0]);
Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());
Graphics graphics = e.Graphics;
//Set Image dimension - User's choice
int iconHeight = 14;
int iconWidth = 14;
//Set x/y position - As the center of the RowHeaderCell
int xPosition = e.RowBounds.X + (dataGridView1.RowHeadersWidth / 2);
int yPosition = e.RowBounds.Y +
((dataGridView1.Rows[e.RowIndex].Height - iconHeight) / 2);
Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
graphics.DrawIcon(myIcon, rectangle);
}