2016-04-13 43 views
0

我可以使用这个在DataGridView中创建Hyperlink;用Datagridview中的按钮替换超链接?

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

    } 

是否可以用图像/按钮替换hyperlink文本?

+0

是的,你可以使用[适当:

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

要通过Tag使用这样的代码访问值列类型](https://msdn.microsoft.com/en-gb/library/bxt3k60s(v = vs.110).aspx) – stuartd

回答

1

那么你当然可以做到这一点。

这里是做它只有一个细胞一个例子:

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

现在你需要决定ButtonText应该是什么:它会显示CellValue。这可能是好的,但如果你的文件名包含一个漫长的道路等。你可能要显示一个较短的文本,并在cell.Tag,而不是存储的名称:如果你想整列中显示的按钮,你

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

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

可以这样做:

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

相同的余数适用于按钮列中的单元格。

要测试,如果你是一个纽扣电池,你可以这样写:

if (cell.Tag != null) string filename = cell.Tag.ToString();