簡體   English   中英

更改DataGridViewLinkColumn的顯示值

[英]Change Display Value of DataGridViewLinkColumn

我有一個datagridview,其中datagridviewlinkcolumn綁定到對象列表中的文本鏈接。 文本鏈接是指向文件的鏈接,這些文件被深埋在網絡存儲中,從而形成了長鏈接。 有什么方法可以更改linkcolumn的鏈接顯示值以僅顯示每個完整鏈接的一部分? 即-僅文件名本身?

我讀過您可以為鏈接列的顯示值使用相同的標題文本,但是我想知道所有顯示值是否可以不同。

因此,總而言之,是否可以在linkcolumn中顯示文件鏈接的一部分,而我要顯示的所有部分都不同,並且仍然具有指向完整文件路徑的實際鏈接?

我找到了一種更好的方法來實現這一目標。

首先,在創建DataGridViewLinkColumnUseColumnTextForLinkValue = false設置UseColumnTextForLinkValue = false

DataGridViewLinkColumnText屬性設置為深埋在網絡存儲中的文件的完整路徑

現在,處理DataGridView CellFormatting事件,並將單元格的Value屬性設置為鏈接顯示名稱。

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
        }
    }

要在單擊鏈接時執行任何操作,您需要按以下方式處理DataGridView CellContentClick事件

    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
        }
    }

弄清楚了。

不知道是否有更好的方法可以執行此操作,但是我在對象中添加了鏈接的簡化版本,並且在我的dataGridView1_CellContentClick事件中,我獲得了與行關聯的對象(dataGridView1.Rows[e.RowIndex].DataBoundItem) ,然后在返回的對象中的標准文件路徑上調用System.Diagnostics.Process.Start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM