我有一个包含以下数据的datagridview。
ContactType | Contact
------------------------------------
Phone | 894356458
Email | xyz@abc.com在这里,我需要将数据"xyz@abc.com“显示为一个超链接,并带有一个工具提示”单击发送电子邮件“。号码数据"894356458“不应该有超链接。
有什么想法吗?
蒂娅!
发布于 2012-06-05 19:55:29
DataGridView对此有一个列类型,即DataGridViewLinkColumn。
您需要手动绑定此列类型,其中DataPropertyName在网格的数据源中设置要绑定到的列:
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.DataPropertyName = "Contact";
col.Name = "Contact";
dataGridView1.Columns.Add(col);您还需要隐藏来自网格的“联系人”属性的“自动生成的文本”列。
此外,与DataGridViewButtonColumn一样,您需要通过响应CellContentClick事件来自己处理用户交互。
若要将不是超链接的单元格值更改为纯文本,需要将链接单元格类型替换为textbox单元格。在下面的示例中,我在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();
}
}
}您也可以从另一个方向执行此操作,将DataGridViewTextBoxCell更改为DataGridViewLinkCell我建议在第二个方向进行,因为您需要将应用于每个单元格的所有链接的更改应用到每个单元格。
但这样做的好处是,您不需要隐藏自动生成的列,因此可能最适合您。
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;
}
}
}发布于 2019-05-30 08:33:10
您可以在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;https://stackoverflow.com/questions/10896623
复制相似问题