简体   繁体   中英

Set size of icon in DataGridViewImageColumn

I am trying to set the size of icons in a column within a datagridview:

// .. 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;

My DataGridView is showing fine with a column on the left with an icon for each row. All icons are of the same size as expected but they look too large to be 8x8 pixels even though I specify my own size. How do I control the icon size?

由于某种原因, 忽略Icon构造函数中传递的Size ,我认为您可以尝试使用Image属性来代替:

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

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

DataGridViewImageColumn.ImageLayout = DataGridViewImageCellLayout.Normal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM