2012-04-02 47 views
0

我想弄清楚如何讓我的c#winform項目中的有界datagridview列顯示像一個href鏈接。事情是,鏈接點擊的作品,但任何普通用戶不會意識到,他們可以點擊該字段,因爲它顯示爲一個字符串。我需要該領域顯示爲藍色,並帶有下劃線,鼠標指針變成一隻手......等等。datagridview使用帶有列href鏈接的數據表

我之前在Datagrid中使用數據集時能夠完成此操作。我去了設計師,選擇「添加列」,並將其添加爲「DataGridViewLinkColumn」。我最近更改了項目以使用數據表,並且我意識到這些字段不再顯示爲可點擊(如果點擊它,確實可行)。

任何理想如何比較容易地做到這一點?我已經搜查,我有些詫異,我似乎無法找到一個簡單的解決方案。

+0

告訴我們,用來定義鏈接欄的代碼。也許你錯誤地將列的類型更改爲DataGridViewTextColumn?它也支持點擊。 – surfen 2012-04-02 22:40:27

回答

0

看看在DataGridViewLinkColumn.LinkBehavior屬性。它可以設置爲AlwaysUnderline。

至於顏色,只需使用DataGridViewLinkColumn上的* LinkColor屬性。

乾杯

+0

謝謝,我明天可能會嘗試你的理想,因爲我試圖用另一種方式來排序和排列順序。任何關於改變指針的想法?我正在考慮使用mouseover/mouseout和cursor.something(對不起,它會讓我不知所措)。這可能會奏效,我會讓你知道的。謝謝您的幫助。 – Chris 2012-04-03 02:48:13

1

這可能幫助:

 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); 

它創建了一個山坳,然後鍵入鏈接的單元格。 您可以使用foreach循環爲更多項目更有序,更快速地完成此操作。

祝你好運!

+0

由於某種原因,我結束了一個空的新列。儘管我在DataGridViewLinkColumn上進行了一些搜索,並且能夠按照如下方式使用它: DataGridViewLinkColumn dgvlc = new DataGridViewLinkColumn(); dgvlc.DisplayIndex = 5; dgvlc.DataPropertyName =「DataboundColumn」; //綁定到正確的數據列 dgvlc.HeaderText =「MyLink」; this.dgvReminderSettings.Columns.Add(dgvlc); – Chris 2012-04-03 02:44:09

+0

我忽略了最初提到這是一個數據綁定網格,但我做了一些更多的搜索,也得到了這個工作。我在上面包含了這部分代碼。 我唯一的其他問題是,我的新專欄坐在網格的最左邊(作爲第一欄而不是第五欄),而且欄不能像其他欄一樣排序。我試過dgvlc.DisplayIndex = 5;把柱子移過去和其他一些東西,但沒有運氣。 如何解決這些小而重要的問題的任何理想? 非常感謝, 〜克里斯 – Chris 2012-04-03 02:44:29

0

您可以在datagridview中爲該列着色。你可以這樣做在DataBindingComplete事件,像這樣:

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

您可以設置字體爲但是你喜歡它(即下劃線,顏色等)。

或者,如果您有預製列(不是自動生成的列),則可以更改設計器中的默認單元格樣式。

2

變化是鏈接是一個DataGridViewLinkCell然後處理單擊該單元格,這樣的細胞類型:

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); 
    } 
} 
+0

這工作對我來說,除了我不得不在數據綁定處理程序中用'r.Cells [「Links」] = new DataGridViewLinkCell {Value = r.Cells [「Links 「] .Value};' – hillstuk 2015-04-24 09:26:07