【问题标题】:Displaying icon files in datagridview在 datagridview 中显示图标文件
【发布时间】:2015-01-09 19:58:49
【问题描述】:

我想在 datagridview 列中显示图标文件,但遇到了一些问题:
see this pic

这是我的代码:

Icon ico = Icon.ExtractAssociatedIcon("C:/1419608299489.jpg");
DataGridViewImageColumn image = new DataGridViewImageColumn();
image.HeaderText = "Image";
image.Icon = ico;
dataGridView1.Columns.Add(image);

【问题讨论】:

  • "C:/1419608299489.jpg" 这行不通。假设您在文件系统中,请在字符串前面加上和号 @ 并使用反斜杠!见MSDN!
  • 抱歉没有解决问题。

标签: c# winforms datagridview


【解决方案1】:

我认为这就是您要搜索的内容(在此处找到 dataGridView Image not displaying):

        Icon icon = Icon.ExtractAssociatedIcon(@"filepath");

        List<MyItem> items = new List<MyItem>();
        for (int i = 0; i < 10; i++)
        {
            items.Add(new MyItem { Key = i, value = icon.ToBitmap() });
        }

        this.dataGridView1.AutoGenerateColumns = false;
        this.dataGridView1.Columns.Clear();
        this.dataGridView1.Columns.Add("Key", "Key");
        this.dataGridView1.Columns.Add(new DataGridViewImageColumn() { HeaderText = "Status" });

        this.dataGridView1.Columns[0].DataPropertyName = "Key";
        this.dataGridView1.Columns[1].DataPropertyName = "value";

        this.dataGridView1.DataSource = items;

MyItem是……。像这样:

class MyItem
{
    public int Key { get; set; }

    public Image value { get; set; }
}

结果:

【讨论】:

  • 抱歉还是同样的问题
  • 但是您检查了那里的文件是确定的吗?因为好像没有找到图片或者什么的。
  • 没有文件,我自己放的只是为了测试功能。
  • Image image = Image.FromFile("yourfile.jpg");对我来说很好。
  • 但我只想要文件的图标而不是图像,因为有时它是 pdf 文件。
【解决方案2】:

检查以下内容:

  • 图像文件肯定在那里。
  • 图像文件未损坏(您可以使用图像查看器访问它)。
  • 您有权访问驱动器 C:(如果没有,请以管理员身份运行程序)。

如果上述情况成立,请尝试将文件复制到项目中。我实际上不明白为什么您要将图像文件保留在项目之外。这进一步简化了事情,因为您可以使用相对路径来访问文件。

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多