Skip to main content
 首页 » 编程设计

c#之Infragistics UltraGrid 中的超链接单元格

2024年02月20日12jiqing9006

我有一个 UltraGrid,它绑定(bind)到一个具有两列(键、值)的 DataTable。我已将 10 行添加到数据表中,现在第 11 行的值列中有一个 URL。 URL 值添加得很好,但它不像超链接那样工作。要使其作为超链接工作,我需要如何将这一行添加到 UltraGrid 中? 我的代码:

DataTable dt = new DataTable(); 
dt.Columns.Add("Key", typeof(string)); 
dt.Columns.Add("Value", typeof(string)); 
ultraGrid.DataSource = dt; 
 
foreach (KeyValuePair<string, string> kvp in dictionary) 
{ 
    dt.Rows.Add(kvp.Key, kvp.Value); 
} 
 
// Adding the row which has the URL value. 
string url = "SomeURL"; 
Uri hyperLink = new Uri(url); 
dt.Rows.Add("Click this", hyperLink); 

请您参考如下方法:

虽然 U1199880 给出的答案指向部分正确的解决方案,但将该样式应用于整个专栏时存在问题。列中的每个单元格都将被视为一个链接。

相反,您需要拦截 InitializeRow 事件并检查当前行的当前单元格是否是有效的 URI。然后将单元格样式属性更改为 ColumnStyle.URL

private void grd_InitializeRow(object sender, InitializeRowEventArgs e) 
{ 
    if (e.ReInitialize == false) 
    { 
        UltraGridColumn c = e.Row.Band.Columns["Value"]; 
        string link = e.Row.GetCellValue(c).ToString(); 
        if (Uri.IsWellFormedUriString(link, UriKind.Absolute)) 
            e.Row.Cells["Value"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.URL; 
    } 
}