根据列ID显示特定图像 [英] Display specific image based on column ID

查看:45
本文介绍了根据列ID显示特定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带datagridview的asp.net Webform C#.每个ID号都有一个jpg图像文件,保存为ID字段的确切编号.
基本上,我想做的是找到一种基于选定的行ID列显示图像的方法.有数百行,如何获取基于ID的图像呢?

I have an asp.net webform C# with a datagridview. There is a jpg image file for every ID # that is saved as the exact number of the ID field.
Basically what I want to do is find a way to display the image based on the selected rows ID column. There are hundreds of rows so how can I get it to select the image based on the ID?

例如,我有3列:

ID  | FirstName | LastName |
----------------------------
2324| John      | Doe      |
2034| Jane      | Doe      |
2946| Mike      | Blank    |

假设选择了Mike Blank.我想在页面顶部显示图像2946.jpg(因为那是他的ID号).

Lets's say Mike Blank is selected. I want to display the image 2946.jpg (because that's what his ID # is) on the top of the page.

注意:所有图像均保存在同一文件夹中.

Note:All images are saved in the same folder.

推荐答案

此代码将在索引0处插入一个图像列,获取存储在 C:\ Images \ [userID] .jpg 中的图像,它将在单元格点击中加载它们.总体思路保持不变,您需要添加一个 DataGridViewImageColumn ,然后您可以随时随地填充该代码.

This code will insert an image column at index 0, grab the image stored in C:\Images\[userID].jpg, and it will load them on cell click. The general idea remains the same, you need to add a DataGridViewImageColumn, which you can then populate however or whenever you'd like.

private void Form1_Load(object sender, EventArgs e)
{
    this.usersTableAdapter.Fill(this.testDBDataSet.Users);
    DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
    dataGridView1.Columns.Insert(0, imageCol);
    dataGridView1.Columns[0].Name = "Image";
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int userID = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
    Bitmap img = new Bitmap(@"C:\Images\" + userID + ".jpg");    
    dataGridView1.Rows[e.RowIndex].Cells[0].Value = img;
}

它产生如下输出:

要将图像添加到表单的其他位置,您需要在表单中创建一个 PictureBox ,然后在行上单击(或在任何时候要更新图像的情况下),只需设置eg

To add an image somewhere else in the form, you need to create a PictureBox to your form, and on the row click (or whenever you want to update the image), simply set e.g.

int userID = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
pictureBox1.ImageLocation = @"C:\Images\" + userID + ".jpg";

这篇关于根据列ID显示特定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆