在 DataGridViewImageColumn 中设置图标的大小

本文关键字:图标 设置 DataGridViewImageColumn | 更新日期: 2023-09-27 18:32:06

我正在尝试在数据网格视图中的列中设置图标的大小:

// .. previous code to fill a dataset with rows of data ...
// Assign dataset into the DataGridView
this.dataGridView1.DataSource = thisDataSet.Tables[0];
// Create icon resource
Icon icon1 = new Icon(SystemIcons.Exclamation, 8, 8);            
// Create a column to hold an icon
DataGridViewImageColumn img = new DataGridViewImageColumn(true);
// Set icon for all rows in the column
img.Icon = icon1; 
// Add column to right side of the DataGridView
this.dataGridView1.Columns.Add(img);
// Move it to the beginning (left side)
this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].DisplayIndex = 0;

我的 DataGridView 显示正常,左侧有一列,每行都有一个图标。所有图标的大小都与预期相同,但即使我指定了自己的大小,它们看起来也太大而无法达到 8x8 像素。如何控制图标大小?

在 DataGridViewImageColumn 中设置图标的大小

由于

某种原因,Icon构造函数中传递的Size忽略了,我认为您可以尝试使用Image属性,如下所示:

img.Image = new Bitmap(SystemIcons.Exclamation.ToBitmap(), 8, 8);

您需要将 ImageLayout 属性设置为 Normal。

DataGridViewImageColumn.ImageLayout = DataGridViewImageCellLayout.Normal