C# 如何在datagridview列标题中显示图像?
在运行时,我将向windows窗体添加一个C# 如何在datagridview列标题中显示图像?,c#,vb.net,winforms,datagridview,C#,Vb.net,Winforms,Datagridview,在运行时,我将向windows窗体添加一个DataGridView。最后一列是一个DataGridViewImageColumn: Dim InfoIconColumn As New DataGridViewImageColumn MyDataGridView.Columns.Insert(MyDataGridView.Columns.Count, InfoIconColumn) 添加以下代码将使“我的信息”图标(位图)显示在每个列单元格中,但不显示在列标题中: Dim InfoIcon As
DataGridView
。最后一列是一个DataGridViewImageColumn
:
Dim InfoIconColumn As New DataGridViewImageColumn
MyDataGridView.Columns.Insert(MyDataGridView.Columns.Count, InfoIconColumn)
添加以下代码将使“我的信息”图标(位图)显示在每个列单元格中,但不显示在列标题中:
Dim InfoIcon As New Bitmap("C:\MyPath\InfoIcon.bmp")
InfoIconColumn.Image = InfoIcon
class YourDataGridViewColumnHeaderCell : System.Windows.Forms.DataGridViewColumnHeaderCell
{
// Set up image as you want
System.Drawing.Image Image { get; set; }
}
此外,值得注意的是,图像在单元格中显示“完美”,即图像大小正确,适合单元格
但是,我找不到向列标题单元格添加相同图像的方法。在谷歌搜索之后,我使用了以下代码,将图像放在标题单元格中,但给我留下了两个问题:
\u CellPaint
事件降低性能,即当将鼠标悬停在DataGridView
上以高亮显示所选行时,高亮显示会滞后于鼠标放置的位置Private Sub MyDataGridView_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles MyDataGridView.CellPainting
Dim InfoIcon As Image = Image.FromFile("C:\MyPath\InfoIcon.bmp")
If e.RowIndex = -1 AndAlso e.ColumnIndex = MyDataGridView.Columns.Count - 1 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
e.Graphics.DrawImage(InfoIcon, e.CellBounds)
e.Handled = True
End If
End Sub
有人知道如何解决我的问题,并在运行时将一个大小合适、清晰的图像放入
DataGridViewImageColumn
headercell吗?一种方法是使用CellsPainting
事件绘制
特定标题单元格的位图。下面是执行此操作的代码
假设位图位于图像列表中
//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;
}
}
请尝试以下代码:
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
Handles DataGridView1.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex = DataGridView1.Columns.Count - 1 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
e.Graphics.DrawImage(IconImg, e.CellBounds)
e.Handled = True
End If
End Sub
如果您已经阅读了这篇文章的全文,请检查此链接:
我需要一件更复杂的事情——在某些列标题中的文本前面添加图像,以实现列对齐
您需要实现自己的并替换ColumnHeaderCell
:
// Create header and set up image
YourDataGridViewColumnHeaderCell headerCell = new YourDataGridViewColumnHeaderCell();
headerCell.Image = something;
// Create column
DataGridViewColumn yourColumn = new DataGridViewTextBoxColumn();
// ...
yourColumn.ColumnHeaderCell = new headerCell;
现在是有趣的部分(列标题的实现):
现在我们要添加方法。唯一棘手的部分是如何处理
如果要使用“设置为”(以便自动调整图像和文本),则需要覆盖。我是通过使用基本实现并向其中添加Image.Width+Padding
来实现的
protected override Size GetPreferredSize( Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex,Size constraintSize )
{
// Load up original image
Size original = base.GetPreferredSize( graphics, cellStyle, rowIndex, constraintSize );
// Ensure the image is set and that we are working on header
if ((rowIndex != -1) || (Image == null)) {
return original;
}
// -1 is reserved value
if (original.Width < 0) {
return original;
}
return new Size( original.Width + Image.Width + 4, original.Height );
}
受保护的覆盖大小GetPreferredSize(图形图形、DataGridViewCellStyle、int rowIndex、大小约束大小)
{
//加载原始图像
原始大小=base.GetPreferredSize(图形、单元格样式、行索引、约束大小);
//确保图像已设置,并且我们正在处理标题
if((rowIndex!=-1)| |(Image==null)){
归还原件;
}
//-1为保留值
如果(原始宽度<0){
归还原件;
}
返回新尺寸(原始.宽度+图像.宽度+4,原始.高度);
}
注意:我花了好几个小时钻研,直到找到答案。希望您不必这样做。试试这个:
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex > -1 Then
If e.ColumnIndex = -1 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.Focus And Not DataGridViewPaintParts.ContentForeground)
DataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
e.Graphics.DrawImage(IconImg, e.CellBounds)
e.Handled = True
'DataGridView1.RowHeadersWidth = 100
'DataGridView1.ColumnHeadersHeight = 25
End If
End If
如果您未将图像存储在ImageList中,请将最后2个LOC修改为;e、 图形.绘图图像(图像,pt);也可以。它可以工作。但是我如何将图像放在文本旁边并添加一些填充?我们可以在datagridview列标题上方添加面板控件吗?@KhurramAli我从未测试/尝试过。。。我认为这应该是可能的。。。如果您要测试它,请告诉我它是否有效。@Vyktor DataGridViewColumn没有这样的属性ColumnHeaderCell。你能澄清一下吗?@user3700562根据2.0版开始提供。但它也注意到它是[BrowsableAttribute(false)]
,因此您无法在designer中看到它(但必须以编程方式进行更改)。您应该解释您正在做什么。我很幸运,我尝试了你的代码,因为e.Paint(e.CellBounds,DataGridViewPaintParts.Focus而不是DataGridViewPaintParts.ContentForeground)正是我想要的。
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex > -1 Then
If e.ColumnIndex = -1 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.Focus And Not DataGridViewPaintParts.ContentForeground)
DataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
e.Graphics.DrawImage(IconImg, e.CellBounds)
e.Handled = True
'DataGridView1.RowHeadersWidth = 100
'DataGridView1.ColumnHeadersHeight = 25
End If
End If