DataGridViewLinkColumn格式

时间:2015-01-11 10:38:57

标签: vb.net winforms datagridview datagridviewlinkcolumn

我的应用程序是VB中的Windows窗体应用程序。

我的应用程序中有DataGridView。当我设计DataGridView时,第七列被定义为DataGridViewLinkColumn。我的应用程序从表中读取链接,并且Grid正确显示它。

我不希望我的用户看到该链接,我希望他们看到像“点击此处访问”这样的句子,但我无法做到。

其次,当我点击链接时没有任何反应。我知道我必须在CellContentClick事件中处理这个问题,但我不知道如何调用指向该链接的默认浏览器。

提前致谢。

1 个答案:

答案 0 :(得分:0)

DataGridViewLinkColumn中没有直接属性分隔显示文本和网址。

要实现目标,您需要处理两个事件CellFormattingCellContentClick。订阅这些活动。

CellFormatting事件处理程序中,将格式化的值更改为Click here to visit。标记FormattingApplied必须设置为True,因为这会阻止进一步格式化值。

Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
    If e.ColumnIndex = 'link column index Then
        e.Value = "Click here to visit";
        e.FormattingApplied = True;
    End If
End Sub

要在默认浏览器中打开链接,请使用Process类并将url作为参数传递给Start方法。将代码放在CellContentClick事件处理程序中。

Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
    If e.ColumnIndex = 'link column index Then
        Process.Start(dataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString());
    End If
End Sub