There is a dataGridView in which the database is displayed. I want a double click on a cell to open an image that is stored as a link in a cell. I have the code:

private void dataGridView1_DoubleClick(object sender, EventArgs e) { var link = dataGridView1.SelectedCells; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = Image.FromFile(link); } 

An error occurs: Error 2 Argument "1": type conversion from "System.Windows.Forms.DataGridViewSelectedCellCollection" to "string" is impossible

Tell me how to write the code correctly.

I tried another version:

  string p = Convert.ToString(dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString()); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = Image.FromFile(p); 

Also does not work.

  • dataGridView1.SelectedCells class instances are in the dataGridView1.SelectedCells collection? What fields are there? - tym32167

1 answer 1

Instead of a double-click event for the whole grid, you should use the double-click event on a specific cell.

 private void DataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.ColumnIndex == 1) // индСкс колонки с линком { var link = dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString(); //pictureBox.Load(link); //pictureBox.LoadAsync(link); } } 

If the value in the cell is a valid url, it can simply be passed to the pickupbox's Load method (or LoadAsync , if the download will be long, for example, from the Internet).

  • What I did not understand, how does the proposed code solve the problem of the author? He will not reveal anything, will not show anything - tym32167
  • @ tym32167 - if there is a link to a picture in the cell (and judging by the other questions of the author, this is the case), PictureBox will download and show the image itself. - Alexander Petrov
  • I realized, I just did not pay attention at first not the commented code - tym32167
  • Thank you! Link received in this way. Only the image is not displayed in this way, it turned out to bring it with: - Georg96
  • pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = Image.FromFile (link); - Georg96