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

2.3k Views Asked by At

i have this scenario :

after query i have this table in DataSet :

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


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

now in this situation i have at 4 columns type text(string): Name,Module,Date,Approvation...

I want column Module is a link to file... then xxx is a link, yyy is a link ... and other..

i have look DataGridViewLinkColumn but i don't know if that's a good way and how to set ..

1

There are 1 best solutions below

5
On BEST ANSWER

The DataGridViewLinkColumn is the way to go, and implementing it should be as simple as:

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

Main source of this answer came from this SO answer, minus the conditional check. The other answers are also informative.