vb.net 如何获取Datagridview Image Cell的图像值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24958603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get image value of Datagridview Image Cell
提问by user3877422
I have an datagridview with playimage ImageColumn and if user click play icon then CellClickEvent set "Stop" image from Resources.
我有一个带有播放图像 ImageColumn的数据网格视图,如果用户单击播放图标,则CellClick事件从资源设置“停止”图像。
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
dg1.Rows(e.RowIndex).Cells(3).Value = New Bitmap(app1.My.Resources.stop)
End Sub
But i need get user what image click (Play or Stop) How to get datagridview image name for Cellclick event? I tried
但我需要让用户点击什么图像(播放或停止) 如何获取 Cellclick 事件的 datagridview 图像名称?我试过
dg1.Rows(e.RowIndex).Cells(3).Value.ToString()
But returned "System.Drawing.Image" value i need image value. Sorry for my bad english.
但返回“ System.Drawing.Image”值我需要图像值。对不起,我的英语不好。
Thanks for interest
感谢关注
采纳答案by Moop
This is how I would get the image. First check if the cell you click is of type DataGridViewImageCell. If it is, try casting the Valueproperty as whatever image format you are expecting.
这就是我获得图像的方式。首先检查您单击的单元格是否为 类型DataGridViewImageCell。如果是,请尝试将Value属性转换为您期望的任何图像格式。
{
dataGridView1.Columns.Add(new DataGridViewImageColumn());
BitMap bitMap = new BitMap(5,5); // or however you get it from resources
bitMap.Tag = "Play"; // Put the name of the image here
dataGridView1.Rows.Add(bitMap);
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
if (dgv == null)
return;
var imageCell = dgv[e.ColumnIndex, e.RowIndex] as DataGridViewImageCell;
if (imageCell == null)
return;
var image = imageCell.Value as Bitmap;
if (image == null)
return;
string name = image.Tag as String;
}
Alternatively, you could save the bitmap as a class level variable:
或者,您可以将位图保存为类级别变量:
Bitmap playBitmap = New Bitmap(app1.My.Resources.play);
Bitmap stopBitmap = New Bitmap(app1.My.Resources.stop);
then in the CellClickmethod:
然后在CellClick方法中:
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
if (dgv == null)
return;
var imageCell = dgv[e.ColumnIndex, e.RowIndex] as DataGridViewImageCell;
if (imageCell == null)
return;
if(imageCell.Value == playBitmap)
{
}
else if (imageCell.Value == stopBitmap)
{
}
}

