我正在寻找将数据网格图像单元格与文本单元格合并的单元格绘制代码,例如顶部的图像和底部的文本。 我被要求找到一种向人们展示职称和部门图片的方式。我能够将职位和部门合并到一个单元格中,但不能将文本合并到文本中。
答案 0 :(得分:2)
以最简单的形式,您可以像这样对CellPainting
事件进行编码:
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0) return; // no image in the header
if (e.ColumnIndex == yourImageColumn )
{
e.PaintBackground(e.ClipBounds, false); // no highlighting
e.PaintContent(e.ClipBounds);
// calculate the location of your text..:
int y = e.CellBounds.Bottom - 35; // your font height
e.Graphics.DrawString(yourText, yourFont, yourColor, e.CellBounds.Left, y);
// maybe draw more text with other fonts etc..
..
e.Handled = true; // done with the image column
}
}
要设置图像单元格的大小和对齐方式,可以在设置图像后使用这样的代码:
// add space for two lines:
dataGridView1.Rows[0].Height = ((Image)dataGridView1[0, 0].Value).Height + 35;
// if the previous line throws an error..
// .. because you didn't put a 'real image' into the cell try this:
// dataGridView1.Rows[0].Height =
((Image)dataGridView1[0, 0]. FormattedValue).Height + 35;
// align the image top left:
dataGridView2[0, 0].Style.Alignment = DataGridViewContentAlignment.TopLeft;
显然,你必须根据你想要使用的字体和字体大小调整数字。
以下是图像下方两行文字的结果:
另一个值得考虑的选择可能是将Image
与文本结合起来......