Winforms DataGridView中的超链接单元格 [英] Hyperlink cell in Winforms DataGridView

查看:194
本文介绍了Winforms DataGridView中的超链接单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的datagridview。

I have a datagridview with the following data.

ContactType        |        Contact
------------------------------------
Phone              |       894356458
Email              |     xyz@abc.com

在这里,我需要将数据xyz@abc.com显示为一个超链接,带有工具提示点击发送电子邮件。数字数据894356458不应该有超链接。

Here, I need to display the data "xyz@abc.com" as a hyperlink, with a tooltip "Click to send email". The number data "894356458" should not have a hyperlink.

任何想法???

TIA!

推荐答案

DataGridView 有一个列类型, DataGridViewLinkColumn

The DataGridView has a column type for this, the DataGridViewLinkColumn.

您需要手动对此列类型进行数据绑定,其中 DataPropertyName 设置要绑定到网格数据源中的列:

You need to databind this column type manually, where DataPropertyName sets the column to bind to in the grid's datasource:

DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.DataPropertyName = "Contact";
col.Name = "Contact";       
dataGridView1.Columns.Add(col);

您还需要隐藏来自网格的Contact属性的自动生成的文本列。

You will also want to hide the autogenerated text column that comes from the Contact property of the grid.

此外,与 DataGridViewButtonColumn 一样,您需要通过响应 CellContentClick 事件。

Also, as with the DataGridViewButtonColumn you need to handle the user interaction yourself by responding to the CellContentClick event.

然后更改不是纯文本超链接的单元格值需要用文本框单元格替换链接单元格类型。在下面的示例中,我在 DataBindingComplete 事件期间完成了此操作:

To then change cell values that are not hyperlinks to plain text you need to replace the link cell type with the textbox cell. In the example below I've done this during the DataBindingComplete event:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        if (!System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
        {
            r.Cells["Contact"] = new DataGridViewTextBoxCell();
        }
    }
}






您也可以从另一个方向执行此操作,将 DataGridViewTextBoxCell 更改为 DataGridViewLinkCell 我建议这样做第二,因为您需要应用适用于每个单元格的所有链接的任何更改。


You can also do this from the other direction, changing the DataGridViewTextBoxCell to a DataGridViewLinkCell I suggest this second since you will need to apply any changes that apply to all links to every cell.

这确实有优势,但你不需要隐藏自动生成的列,所以最适合你。

This does have the advantage though that you will not then need to hide the autogenerated column, so may suit you best.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        if (System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
        {
            r.Cells["Contact"] = new DataGridViewLinkCell();
            // Note that if I want a different link colour for example it must go here
            DataGridViewLinkCell c = r.Cells["Contact"] as DataGridViewLinkCell;
            c.LinkColor = Color.Green;
        }
    }
}

这篇关于Winforms DataGridView中的超链接单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆