为 DataGridView 设置 ToolTip 自动创建的列 [英] Setting ToolTip for DataGridView automatically created columns
问题描述
我想以编程方式将工具提示设置为在 DataGridView
中自动生成的列.我试图使用 AutoGeneratingColumn
事件 (http://msdn.microsoft.com/en-us/library/cc903950%28VS.95%29.aspx),但实际上只能访问DataGridColumn
,不能DataGridViewColumn
,前者没有 ToolTipText
属性.
I would like to programatically set tooltips to automatically generated columns in a DataGridView
.
I was trying to use AutoGeneratingColumn
event (http://msdn.microsoft.com/en-us/library/cc903950%28VS.95%29.aspx), but that in fact can only access DataGridColumn
, not DataGridViewColumn
, and the former doesn't have ToolTipText
property.
或者,如果我可以将工具提示绑定到一个也很棒的源.目标是能够在我为基础 DataTable
设置列的同一位置操作/设置工具提示.
Or if I could bind the ToolTips to a source that would also be great. The goal is to have the ability to manipulate/set tooltips in the same place where I set the columns for the underlying DataTable
.
推荐答案
我是这样解决的:
void payloadDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string tooltip = null;
switch (e.Column.Header.ToString())
{
case "Column 1":
tooltip = "Tooltip 1";
break;
case "Column 2":
tooltip = "Tooltip 2";
break;
}
if (tooltip != null)
{
var style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, tooltip));
e.Column.CellStyle = style;
}
}
这篇关于为 DataGridView 设置 ToolTip 自动创建的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!