【问题标题】:Change DataGridView's cell image based on a property in DataGridView's DataSource根据 DataGridView 的 DataSource 中的属性更改 DataGridView 的单元格图像
【发布时间】:2015-06-15 13:09:04
【问题描述】:

我有一个DataTable,其中包含以下列:

  1. 身份证
  2. 图像索引
  3. 来源
  4. 目的地

在我的表单加载事件中,我想用我的DataTable 填充我的DataGridView。但我还希望有一个图像列,根据我的DataTable 上的 ImageIndex 的值显示不同的图像。

我该怎么做?

【问题讨论】:

    标签: c# winforms datagridview datatable


    【解决方案1】:

    第一步是在属性文件夹下的 Resources.resx 文件中添加图像。然后在 DataGridView 中添加一个 DataGridViewImageColumn。

    最后在你的gridview的数据绑定事件上使用这个sn-p。

    for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
     {
        if(gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 1)
            {
                (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture1
            } 
        else if (gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 2)
            {
                (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture2
            } 
    }
    

    【讨论】:

    • 如果图像已导入 Resources.resx,这将正常工作。我的猜测是图像可能来自存储在数据库中的 URL。如果是这种情况,请查看:stackoverflow.com/questions/10759772/…
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多