Chris
Chris

Reputation: 39

datagridview using a datatable with a column href link

I'm trying to figure out how to get a bounded datagridview column in my c# winform project to show up like an href link. The thing is that the link click works but any average user wouldn't realize that they can click the field since it's displayed as a string. I need the field to show up as blue, with underlines, the mouse pointer turns into a hand ...etc.

I was able to accomplish this previously when I was using Datasets with my Datagrid. I went to the designer and selected "Add Column" and added it as a 'DataGridViewLinkColumn". I've recently changed the project to use datatables and I realized that the fields no longer show up as clickable (if I click it does work though).

Any ideal how to accomplish this with relative ease? I've searched and I'm somewhat surprised that I cannot seem to find a simple solution.

Upvotes: 0

Views: 11714

Answers (4)

noelicus
noelicus

Reputation: 15055

Change the type of the cells that are links to be a DataGridViewLinkCell and then handle the click on the cell, like this:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        if (System.Uri.IsWellFormedUriString(r.Cells["Links"].Value.ToString(), UriKind.Absolute))
        {
            r.Cells["Links"] = new DataGridViewLinkCell();
            DataGridViewLinkCell c = r.Cells["Links"] as DataGridViewLinkCell;
        }
    }
}

// And handle the click too
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
    {
        System.Diagnostics.Process.Start( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
    }
}

Upvotes: 3

ImGreg
ImGreg

Reputation: 2983

You could just colour that column in the datagridview. You could do this in a DataBindingComplete event like so:

private void dataGridView1_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
{
    if(this.mydatagridview.Columns["YourLinkColumnName"] != null)
    {    
        this.mydatagridview.Columns["YourLinkColumnName"].DefaultCellStyle.Font = ...
        this.mydatagridview.Columns["YourLinkColumnName"].DefaultCellStyle.ForeColor = ...
    }
}

You can set the font to be however you like it (ie. underlined, colored, etc.).

Alternatively, you can change the default cell style in the designer if you have the columns premade (not autogeneratedcolumns).

Upvotes: 0

NicoTek
NicoTek

Reputation: 1167

This might help:

        DataGridViewLinkColumn col1 = new DataGridViewLinkColumn();
        dataGridView1.Columns.Add(col1);
        dataGridView1.Columns[0].Name = "Links";

        DataGridViewRow dgvr = new DataGridViewRow();
        dgvr.CreateCells(dataGridView1);

        DataGridViewCell linkCell = new DataGridViewLinkCell();
        linkCell.Value = @"http:\\www.google.com";
        dgvr.Cells[0] = linkCell;

        dataGridView1.Rows.Add(dgvr);

it creates a col and then a cell of type link. you can use foreach loops to do this more orderly and faster for more items.

Good Luck!

Upvotes: 1

Luc Morin
Luc Morin

Reputation: 5380

Take a look at the DataGridViewLinkColumn.LinkBehavior Property. It can be set to AlwaysUnderline.

As for color, simply use the *LinkColor properties on the DataGridViewLinkColumn.

Cheers

Upvotes: 0

Related Questions