2013-02-05 92 views
1

我有一個UltraGrid,它綁定到帶有兩列(鍵值)的DataTable。我在DataTable中添加了10行,現在第11行在Value列中有一個URL。 URL值可以很好地添加,但它不能像超鏈接一樣工作。要使其作爲超鏈接工作,我如何需要將此行添加到UltraGrid中? 我的代碼:Infragistics 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); 

回答

3

儘管U1199880給出的答案指出了部分正確的解決方案,但將該樣式應用於整列時仍有問題。該列中的每個單元格都將被視爲鏈接。

相反,您需要攔截InitializeRow事件並檢查當前行的當前單元格是否爲有效的URI。然後將單元格Style屬性更改爲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; 
    } 
} 
1

當你定義網格列使用類型:Infragistics.Win.UltraWinGrid.ColumnStyle.URL作爲列類型。

然後網格會在你的代碼中產生一個CellLinkClicked事件。

+0

這將更改列中的每個單元格而不僅僅是最後一個。 – Steve

+0

您可以在單元上設置樣式:http://help.infragistics.com/NetAdvantage/WinForms/Current/CLR4.0/?page=Infragistics4.Win.UltraWinGrid.v12.2~Infragistics.Win.UltraWinGrid.UltraGridCell_members .html – alhalama

+0

鏈接被@alhalama打破了評論。基本上你可以做'cell.Style = ColumnStyle.URL'。這裏是[當前文檔]的鏈接(http://help.infragistics.com/Help/Doc/WinForms/2014.2/CLR4.0/html/Infragistics4.Win.UltraWinGrid.v14.2~Infragistics.Win.UltraWinGrid。 UltraGridCell_members.html),希望它不會太早破壞。 (Infragistics需要修復他們的文檔,yeeesh!) –

相關問題