简体   繁体   中英

Change Display Value of DataGridViewLinkColumn

I have a datagridview with a datagridviewlinkcolumn bound to a text link in my list of objects. The text links are links to files, and the files are buried deep in network storage, making for long links. Is there some way I can change the link display value of the linkcolumn to show only a portion of each full link? ie - just the file name itself?

I've read you can use the same header text for the display value of a link column, but I'm wondering if all of the display values could be different.

So to summarize, is it possible to show part of a file link in the linkcolumn, while all of the parts I want to show will be different, and still have the actual link point to the full file path?

I found a better way to achieve this.

First, while creating the DataGridViewLinkColumn set UseColumnTextForLinkValue = false

Set the Text property of the DataGridViewLinkColumn to the full path of the file which is buried deep in the network storage.

Now, handle CellFormatting event of the DataGridView and set the Value property of the cell to the display name of the link

private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        if (excelDataGridView.Columns[e.ColumnIndex].Name.Equals("Links"))
        {
             if(e.Value != null)
                e.Value = Path.GetFileName(e.Value.ToString()); //change the display name for Hyperlink
        }
    }

To perform any action on clicking of the link you need to handle CellContentClick event of DataGridView as follows

    private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if(e.ColumnIndex == excelDataGridView.Columns["Links"].Index) //Handling of HyperLink Click
        {
            string cellValue = excelDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            Process.Start(cellValue); //assumes the link points to the text file and opens it in the default text editor
        }
    }

Figured it out.

Not sure if there's a better way to do this, but I added a shortened version of the link to my object, and in my dataGridView1_CellContentClick event, I get the object associated with the row (dataGridView1.Rows[e.RowIndex].DataBoundItem) , and call System.Diagnostics.Process.Start() on the fully qualified file path in the object returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM