c #datagridview在winform中读取db后的列类型超链接

时间:2016-03-16 09:03:42

标签: c# datagridview datagridviewlinkcolumn

我有这种情况:

查询后我在DataSet中有这个表:

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


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

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

我想要列模块是文件链接 ...然后 xxx 是一个链接, yyy 是一个链接...和其他..

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

1 个答案:

答案 0 :(得分:1)

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,减去条件检查。其他答案也提供了信息。