【问题标题】:Creating responsive hyperlink link in Winform DataGridView在 Winform DataGridView 中创建响应式超链接链接
【发布时间】:2014-03-02 15:04:28
【问题描述】:

我有用 C# 编写的 Winform。

具体来说,我有一个看起来像这样的DataGridView 控件:

No.|Description   | Quantity|   file                  | 
 1 |Beaker        |    2    |c:\PDF\Beaker.pdf        |
 2 |Conical Flask |    21   |c:\PDF\Conical Flask.pdf |

我希望 文件 列包含一个超链接或一个按钮,单击该按钮可在用户计算机上打开 PDF 文件。

提前致谢!

【问题讨论】:

    标签: c# winforms datagridview hyperlink


    【解决方案1】:

    如果我理解您的问题,您想使用的是 DataGridViewLinkColumn。

    然后,只需处理 DataGridView.CellContentClicked 事件。

    在处理程序中,您可以像这样访问单元格内容:

    using System.Diagnostics;
    using System.IO;
    
    ...
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string filename = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            if(e.ColumnIndex == 3 && File.Exists(filename))
            {
                Process.Start(filename);
            }
    
        }
    

    干杯

    【讨论】:

    • 首先我要感谢您的回复...而不是打开消息框,我想打开所需的文件,这是用户喜欢的程序的pdf文件
    • 您没有指定问题的哪一部分特别需要帮助,所以我假设只有链接列部分。我更新了我的示例代码以满足您的需求。
    • 这就是我正在寻找的,当点击的链接通过用户喜欢的程序打开文件时。非常感谢您,您为我节省了过去两天寻找此解决方案所花费的时间和精力,非常感谢您
    • 现在当点击链接打开 pdf 文件,但它是一个 textboxcolmn 不是超链接或按钮怎么做。
    • 我认为创建一个新问题会更好,因为您已经将此问题标记为已回答。
    【解决方案2】:

    如前所述,你需要利用DataGridViewLinkColumn,然后处理DataGridViewCellContentClick事件。

    您将如何做到这一点:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
        {
            Process.Start(this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
        }
    }
    

    编辑

    注册活动很重要。否则,事件处理程序代码将永远不会触发。

    this.dataGridView1.CellContentClick += 
           new System.Windows.Forms.DataGridViewCellEventHandler(
           this.dataGridView1_CellContentClick);
    

    如果出于某种原因按钮是您的首选选项,那么您需要以类似的方式使用DataGridViewButtonColumn

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2014-12-22
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多