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 Value
property as whatever image format you are expecting.
{
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 CellClick
method:
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)
{
}
}