I have an datagridview with play image ImageColumn and if user click play icon then CellClick Event set "Stop" image from Resources.
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
dg1.Rows(e.RowIndex).Cells(3).Value.ToString()
But returned "System.Drawing.Image" value i need image value. Sorry for my bad english.
Thanks for interest
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)
{
}
}