C# Winforms DataGridView 中的超链接单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10896623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hyperlink cell in Winforms DataGridView
提问by Sandeep
I have a datagridview with the following data.
我有一个包含以下数据的 datagridview。
ContactType | Contact
------------------------------------
Phone | 894356458
Email | [email protected]
Here, I need to display the data "[email protected]" as a hyperlink, with a tooltip "Click to send email". The number data "894356458" should not have a hyperlink.
在这里,我需要将数据“[email protected]”显示为超链接,并带有工具提示“单击以发送电子邮件”。数字数据“894356458”不应有超链接。
Any ideas???
有任何想法吗???
TIA!
蒂亚!
采纳答案by David Hall
The DataGridViewhas a column type for this, the DataGridViewLinkColumn.
在DataGridView此有一栏类型的DataGridViewLinkColumn。
You need to databind this column type manually, where DataPropertyNamesets the column to bind to in the grid's datasource:
您需要手动数据绑定此列类型,其中DataPropertyName设置要绑定到网格数据源中的列:
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.DataPropertyName = "Contact";
col.Name = "Contact";
dataGridView1.Columns.Add(col);
You will also want to hide the autogenerated text column that comes from the Contact property of the grid.
您还需要隐藏来自网格的 Contact 属性的自动生成的文本列。
Also, as with the DataGridViewButtonColumnyou need to handle the user interaction yourself by responding to the CellContentClickevent.
此外,与DataGridViewButtonColumn您需要通过响应CellContentClick事件来自己处理用户交互一样。
To then change cell values that are not hyperlinks to plain text you need to replace the link cell type with the textbox cell. In the example below I've done this during the DataBindingCompleteevent:
然后要将非超链接的单元格值更改为纯文本,您需要将链接单元格类型替换为文本框单元格。在下面的示例中,我在DataBindingComplete活动期间完成了此操作:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (!System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Contact"] = new DataGridViewTextBoxCell();
}
}
}
You can also do this from the other direction, changing the DataGridViewTextBoxCellto a DataGridViewLinkCellI suggest this second since you will need to apply any changes that apply to all links to every cell.
您也可以从另一个方向执行此操作,将 更改DataGridViewTextBoxCell为DataGridViewLinkCell我建议的第二个,因为您需要将适用于所有链接的任何更改应用到每个单元格。
This does have the advantage though that you will not then need to hide the autogenerated column, so may suit you best.
这样做的好处是您不需要隐藏自动生成的列,因此可能最适合您。
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Contact"] = new DataGridViewLinkCell();
// Note that if I want a different link colour for example it must go here
DataGridViewLinkCell c = r.Cells["Contact"] as DataGridViewLinkCell;
c.LinkColor = Color.Green;
}
}
}
回答by Saleem Kalro
You can change style of whole column in DataGridView. This is also a way to make column link column.
您可以在 DataGridView 中更改整列的样式。这也是一种制作列链接列的方法。
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
cellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
cellStyle.ForeColor = Color.LightBlue;
cellStyle.SelectionForeColor = Color.Black;
cellStyle.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Underline);
dataGridView.Columns[1].DefaultCellStyle = cellStyle;

