我有一个带有播放图片ImageColumn的数据网格视图,如果用户点击播放图标,则 CellClick 事件设置“停止”图片来自资源。< / p>
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
但我需要让用户点击什么图像(播放或停止) 如何获取Cellclick事件的datagridview图像名称? 我试过了
dg1.Rows(e.RowIndex).Cells(3).Value.ToString()
但返回“ System.Drawing.Image ”值我需要图像值。 抱歉我的英语不好。
感谢您的兴趣
答案 0 :(得分:1)
这就是我得到图像的方式。首先检查您单击的单元格是否为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;
}
或者,您可以将位图保存为类级别变量:
Bitmap playBitmap = New Bitmap(app1.My.Resources.play);
Bitmap stopBitmap = New Bitmap(app1.My.Resources.stop);
然后在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)
{
}
}