0

Im able to create a Hyperlink in a DataGridView using this;

  private void dgvCatalogue_CellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
    {
        string filename = dgvCatalogue[e.ColumnIndex, e.RowIndex].Value.ToString();
        if (e.ColumnIndex == 5 && File.Exists(filename))
        {
            Process.Start(filename);
        }

    }

Is it possible to replace the hyperlink text with an image/button?

AndroidAL
  • 1,111
  • 4
  • 15
  • 35
  • Yes, you would use the [appropriate column type](https://msdn.microsoft.com/en-gb/library/bxt3k60s(v=vs.110).aspx) – stuartd Apr 13 '16 at 13:34

1 Answers1

1

Well you certainly can do that.

Here is an example that does it for one Cell only:

DataGridViewButtonCell bCell = new DataGridViewButtonCell();
dataGridView1[col, row] = bCell;

Now you need to decide what the Button's Text should be: It will show the Value of the Cell. This may be fine, but if your filename includes a lengthy path etc.. you may want to display a shorter text and store the name in the cell.Tag instead :

string someFileName=  "D:\\temp\\example.txt";

dataGridView1[col, row].Tag = someFileName;
dataGridView1[col, row].Value = Path.GetFileNameWithoutExtension(someFileName);

If you want a whole column to display Buttons you can do that like this:

DataGridViewButtonColumn bCol = new DataGridViewButtonColumn();
dataGridView1.Columns.Add(bCol);

The same cosiderations apply to the cells in the button column.

To test if you are on a button cell you can write:

DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
if (cell.OwningColumn.CellType == typeof(DataGridViewButtonCell)) ..

To access the values via the Tag use code like this:

if (cell.Tag != null) string filename = cell.Tag.ToString();
TaW
  • 53,122
  • 8
  • 69
  • 111