【问题标题】:Setting ToolTip for DataGridView automatically created columns为 DataGridView 设置 ToolTip 自动创建的列
【发布时间】:2013-09-09 13:47:51
【问题描述】:

我想以编程方式将工具提示设置为在 DataGridView 中自动生成的列。 我尝试使用AutoGeneratingColumn事件(http://msdn.microsoft.com/en-us/library/cc903950%28VS.95%29.aspx),但实际上只能访问DataGridColumn,不能访问DataGridViewColumn,而且前者没有ToolTipText属性。

或者如果我可以将工具提示绑定到一个也很棒的源。目标是能够在我为基础DataTable 设置列的相同位置操作/设置工具提示。

【问题讨论】:

    标签: c# wpf datagridview tooltip autogeneratecolumn


    【解决方案1】:

    我设法以这种方式解决了它:

    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】:

      特定单元格的工具提示文本:

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

      将工具提示添加到动态添加的行特定单元格

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

      【讨论】:

      • 请围绕您的答案提供一些背景信息。
      猜你喜欢
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2015-04-01
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多