c# datagridview 在 db 读取 winform 后带有列类型超链接

c# datagridview with a column type hyperlink after db read in winform

我有这种情况:

查询后,我在数据集中有这个 table:

Name | Module | Date | Approvation
xx   |  xxx   | xxx  | xxxxxxxx
yy   |  yyy   | yyy  | yyyyyyyyy 


        DataTable dt = new DataTable();
        //  dgvApprovazione is a datagridview
        dgvApprovazione.DataSource = dt

现在在这种情况下,我有 4 列键入文本(字符串):名称、模块、日期、批准...

我想要列模块是 link 到文件 ... 然后 xxx 是 link , yyy 是 link ... 和其他..

我看过 DataGridViewLinkColumn,但我不知道这是否是一个好方法以及如何设置..

DataGridViewLinkColumn 是要走的路,实施起来应该很简单:

this.dataGridView1.CellContentClick += DataGridView1_CellContentClick;
this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;

private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
    {
        System.Diagnostics.Process.Start(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
    }
}

private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        row.Cells["Module"] = new DataGridViewLinkCell();
    }
}

此答案的主要来源来自 this SO answer,减去了条件检查。其他答案也很有参考价值。