DataGridViewImageColumn不显示值-为什么
本文关键字:为什么 显示 DataGridViewImageColumn | 更新日期: 2023-09-27 18:12:15
我想在我的dataGridView中显示一些图像,所以我用默认图像(Properties.Resources.test
)创建了DataGridViewImageColumn
,将其添加到dataGridView
并尝试向单元格插入一些值。不幸的是,它并没有改变显示屏。我做错了什么?
var q = from a in _dc.GetTable<Map>() select a;
View.dataGridView1.DataSource = q;
View.dataGridView1.Columns[3].Visible = false;
var imageColumn = new DataGridViewImageColumn
{
Image = Properties.Resources.test,
ImageLayout = DataGridViewImageCellLayout.Stretch,
Name = "Map",
HeaderText = @"map"
};
View.dataGridView1.Columns.Add(imageColumn);
var i = 0;
foreach (var map in q)
{
View.dataGridView1.Rows[i].Cells[8].Value = ByteArrayToImage(map.map1.ToArray());
i++;
}
正如评论中的问题所解释的那样,您需要像这样使用CellFormatting事件:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "StatusImage")
{
// Your code would go here - below is just the code I used to test
e.Value = Image.FromFile(@"C:'Pictures'TestImage.jpg");
}
}
所以设置e值而不是单元格。
你可以这样做…
for(int i = 0; i < dataGridView1.Columns.Count; i ++)
if(dataGridView1.Columns[i] is DataGridViewImageColumn) {
((DataGridViewImageColumn)dataGridView1.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}