A variable name can't easily be used to access a variable, particularly from a different class. You can do it using reflection, but ... you need the instance of the class that holds the private variable in order to do it, and that makes it even more messy!
That you are trying to do this shows there is something very wrong in your overall design, as this is not a good idea at all!
Instead, create a new class:
public class ImageContainer
{
public string Name { get; set; }
public Image Image { get; set; }
public override string ToString()
{
return Name;
}
}
And fill that with the image and name:
ImageContainer ic = new ImageContainer() { Name = "Hazmat", Image = hazmat };
Then use that class instance in your DataGridView. It will display the Name, but the cell will contain the ImageContainer.
Pass that to your second form:
if (dgv1.Rows[rowID].Cells["ImageName"].Value != null)
{
ImageContainer ic = (ImageContainer) dgv1.Rows[rowID].Cells["ImageName"].Value;
formData.setPic(ic);
}
public void setPic(ImageContainer ic)
{
pb1.Image = ic.Image;
}
Clean, simple, easy to work with.