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();