C#DataGridView 添加超链接

方式1:

DataGridView 的 列设置为 DataGridViewLinkColumn 即可

这种方式 方便快捷  只是在运行后此列不可编辑

方式2:

DataGridView 的列设置为  DataGridViewTextBoxColumn (以此列为第0列为说明)

自己手动封装一个使得此列的内容带有超链接的效果  以假弄真

步骤1、DataGridViewTextBoxColumn 的style属性 设置为带下划线 蓝色字体

步骤2、添加点击内容打开网页事件 ,DataGridView添加事件CellContentClick  

代码如下: 

​
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0)
    { 
        return;
    }
    if (e.ColumnIndex == 0)
    {
        var row = dataGridView1.Rows[e.RowIndex];
        if (row.Cells[0].Value == null) return;
        var url = row.Cells[0].Value.ToString();
        if (UrlTest(url))//是否可以将此文转换为一个网址
        {
            System.Diagnostics.Process.Start(url);//默认浏览器打开此链接
            row.Cells[0].Style.ForeColor = Color.DarkViolet;//点击后变成紫色
        }
    }
}

​

判断内容是否能转换成网址方法

​
private bool UrlTest(string url)
{
    var rvalue = false;
    try
    {
        new Uri(url);
        rvalue = true;
    }
    catch (Exception)
    {}
    return rvalue;
}

​


步骤3、鼠标经过内容时变成手指形状

DataGridView添加鼠标事件

代码如下:

​
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex != -1 )
    {
        var row = dataGridView1.Rows[e.RowIndex];
        if (row.Tag != null)
        {
            if (e.ColumnIndex == 0)
            {
                //窗体的位置
                Point frm_location = this.Location;
                //窗体标题的高度 = 窗体高度 - 工作区域的高度
                int title_H = this.Height - this.ClientRectangle.Height;
                //dataGridView1 在父容器的位置  这里假设dataGridView1的父容器就是窗体本身
                Point dgv_location = dataGridView1.Location;
                //获取文本的宽度  以像素为单的宽度 sizeF.Width  new Font("宋体",9) 根据实际情况修改
                Graphics graphics = CreateGraphics();
                SizeF sizeF = graphics.MeasureString(row.Cells[0].Value.ToString(), new Font("宋体", 9));
                graphics.Dispose();
                //单元格的可见的工作区域 矩形  有时候文本的长度很长 单元格是显示不完的
                Rectangle rec = dataGridView1.GetCellDisplayRectangle(0, e.RowIndex, true);
                //当前鼠标的位置  相对于整个屏幕的位置
                Point p_mouse = MousePosition;
                //触发鼠标变形的起止坐标
                int start_x = frm_location.X + dgv_location.X + rec.Location.X;//frm_location.X 这个可要可不要,为了严谨性 还是加上比较好
                int start_y = frm_location.Y + title_H + dgv_location.Y + rec.Location.Y - 5;
                int end_x = 0;
                int end_y = frm_location.Y + title_H + dgv_location.Y + rec.Location.Y + rec.Height - 15;
                if (sizeF.Width >= dataGridView1.Columns[0].Width) //文本宽度是否大于列宽
                {
                    end_x = frm_location.X + dgv_location.X + dataGridView1.RowHeadersWidth + rec.Width;
                }
                else
                {
                    end_x = frm_location.X + dataGridView1.RowHeadersWidth + (int)sizeF.Width;
                }
                if (p_mouse.X > start_x && p_mouse.Y > start_y && p_mouse.X < end_x && p_mouse.Y < end_y)
                {
                    this.Cursor = Cursors.Hand;//鼠标变成手形
                }
                else
                {
                    this.Cursor = Cursors.Default;//鼠标形状还原默认
                }
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }
        else
        {
            this.Cursor = Cursors.Default;
        }
    }
    else
    {
        this.Cursor = Cursors.Default;
    }
}

​

猜你喜欢

转载自blog.csdn.net/fjfjfjgh/article/details/80701201
今日推荐