【问题标题】:Hyperlink cell in Winforms DataGridViewWinforms DataGridView 中的超链接单元格
【发布时间】:2012-06-09 10:19:45
【问题描述】:

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

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

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

有什么想法吗???

TIA!

【问题讨论】:

  • 我已经编辑了我的答案,解释了如何在你的情况下更好地使用我的第一个选项(通过隐藏一列并使用 DataPropertyName),并在你保留文本的地方提供第二个变体列。

标签: c# datagridview hyperlink


【解决方案1】:

DataGridView 有一个列类型,DataGridViewLinkColumn

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

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

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

此外,与DataGridViewButtonColumn 一样,您需要通过响应CellContentClick 事件自己处理用户交互。


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

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 我建议第二次这样做,因为您需要应用适用于每个单元格的所有链接的任何更改。

这确实有好处,但您不需要隐藏自动生成的列,因此可能最适合您。

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

【讨论】:

  • 顺便说一句,datagridview 的一个好处是,在响应单元格内容单击时,您可以执行诸如在单击电话号码时打开 Voip 服务之类的操作。
  • 或者,你不能反过来吗?像往常一样默认定义网格(TextBoxCell),然后超链接每行中所需的单元格?只是想知道为什么您默认使用 LinkCells 然后显式更改回 TextBox
  • @Brett 超链接列有一些额外的属性和行为很方便,例如访问的链接颜色和 TrackVisitedState - 当然你可以反过来做,但我认为这种方式更方便。
  • 嗯,这个解决方案听起来不错。但是这个 gridview 的数据源是一个 List,其中 myClass 具有属性 - ContactType、Contact。所以它只是将数据源设置为gridview。我不会在这里手动添加行和列。在这种情况下,我们如何解决这个问题?
  • David 可能对此有更好的了解,但如果您的数据库/数据源结构是静态的,您仍然可以手动定义 DataGrid 中的列并进行绑定,而不会覆盖您定义的行为,我相信。
【解决方案2】:

您可以在 DataGridView 中更改整列的样式。这也是一种制作列链接列的方法。

DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
        cellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        cellStyle.ForeColor = Color.LightBlue;
        cellStyle.SelectionForeColor = Color.Black;
        cellStyle.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Underline);
        dataGridView.Columns[1].DefaultCellStyle = cellStyle;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多