Datagridview两列链接应指向不同的事件

本文关键字:事件 链接 两列 Datagridview | 更新日期: 2024-09-25 01:03:38

我有一个带有dataset/SQL数据库的winform数据网格视图。datagridview有一个列,其中有指向网站的链接,这些网站可以很好地处理此事件:

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

我现在需要添加一个新的链接列,其中包含本地文件夹中图片的文件名。由于上面的方法结构,linkClick事件的行为没有区别,但我需要一个columnlink在单击时打开URL,另一个columnlink打开本地文件。我该怎么做?

Datagridview两列链接应指向不同的事件

您已经可以访问ColumnIndex。。。您可以检查当前单击的单元格属于哪一列:

if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
{
    var location = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value);
    if (e.ColumnIndex == 1)
        Process.Start(location);
    else if (e.ColumnIndex == 2)
        // Open the file specified in 'location'
}