Setting ToolTip for DataGridView automatically created columns

梦想的初衷 提交于 2019-11-28 11:51:37

问题


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.

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.


回答1:


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



回答2:


tooltiptext for specific cell:

DataGridView1.Rows[3].Cells["colnameX"].ToolTipText = " hover and see me";

adding tooltip to dynamically added rows specific cell

private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    for (int index = e.RowIndex; index <= e.RowIndex + e.RowCount - 1; index++)                           
    {
        DataGridViewRow row = DataGridView1.Rows[index];
        row.Cells["colnameX"].ToolTipText = " hover and see me";

    }
}


来源:https://stackoverflow.com/questions/18699717/setting-tooltip-for-datagridview-automatically-created-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!