在c#程序中使用cellcontentclick事件来激活链接

时间:2013-03-27 17:09:10

标签: c# datagridview hyperlink

我有一个程序,它使用带有7列的datagridview。其中一列是一个超链接,用于从指定位置加载文件。我使用'cellcontentclick'事件打开文件。我的问题是,当我单击行中的任何其他单元格时,它仍将执行cellcontentclick。只有当点击该特定列时才能执行此操作?

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            string sourcePath = @"SPECIFIED PATH";

            Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value);
        }

        catch (SqlException e)
        {
            MessageBox.Show("Error occured: " + e);
        }
    }

2 个答案:

答案 0 :(得分:2)

仅针对您要查找的列检查内部事件处理程序。 其中一个参数(e?)有列信息。

答案 1 :(得分:1)

知道了!我只需要输入if语句并指定列。谢谢你,evgenyl。

        if (e.ColumnIndex == 5 && e.RowIndex >= 0)
        {
            try
            {
                string sourcePath = @"PATH";

                Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value);
            }

            catch (SqlException a)
            {
                MessageBox.Show("Error occured: " + a);
            }
        }
相关问题