问题描述
我想以 DataGridView
中的自动生成列的方式将工具提示设置为编程方式。
我试图使用 AutoGeneratingColumn
事件(),但实际上只能访问 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
.
推荐答案
我以这种方式解决了这个问题:
I managed to solve it this way:
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设置工具提示自动创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!