工具提示未显示在DataGridViewCheckBoxColumn上

时间:2012-05-23 14:24:05

标签: c# winforms

我正在向DataGridViewCheckBoxColumn动态添加DataGridView

但是,我似乎无法将工具提示添加到复选框:

// Method to Populate the DataGrid
    private void PopulateDataGrid(objPatient patient)
    {
        this.uiDocumentDataGrid.DataSource = DataManager.GetDocumentData(patient);
        // Hide unnecessary columns
        this.uiDocumentDataGrid.Columns["Forename"].Visible = false;
        this.uiDocumentDataGrid.Columns["Surname"].Visible = false;

        // Add column for selection
        DataGridViewCheckBoxColumn selectedColumn = new DataGridViewCheckBoxColumn();
        selectedColumn.Name = "Selected";
        selectedColumn.HeaderText = "Attach";
        selectedColumn.ReadOnly = false;
        this.uiDocumentDataGrid.Columns.Insert(0,selectedColumn);

        // Set columns except checkbox to readonly
        foreach(DataGridViewColumn c in this.uiDocumentDataGrid.Columns)
        {
            if (c.Index > 0)
            {
                c.ReadOnly = true;
            }
        }

        // Refresh the view in case of draw issues
        this.uiDocumentDataGrid.Refresh();

        // Add tooltip to each checkbox
        foreach (DataGridViewRow r in uiDocumentDataGrid.Rows)
        {
            r.Cells["Selected"].ToolTipText = "Check this box to select this document.";
        }

        // Disable the functionality button if no rows.
        if (this.uiDocumentDataGrid.RowCount == 0)
        {
            this.uiSendButton.Enabled = false;
        }

    }

此方法没有显示工具提示。我错过了一些明显的东西吗?

3 个答案:

答案 0 :(得分:1)

不要尝试在表单的构造函数中设置单元格的ToolTip值。请尝试在加载或显示事件中设置它们:

protected override void OnLoad(EventArgs e) {
  foreach (DataGridViewRow r in uiDocumentDataGrid.Rows)
  {
    r.Cells["Selected"].ToolTipText = "Check this box to select this document.";
  } 
  base.OnLoad(e);
}

答案 1 :(得分:0)

试试这个

   DataGridViewCheckBoxColumn selectedColumn = new DataGridViewCheckBoxColumn();
   selectedColumn.Name = "Selected";
   selectedColumn.HeaderText = "Attach";
   selectedColumn.ReadOnly = false;
   this.uiDocumentDataGrid.Columns.Insert(0,selectedColumn);
   uiDocumentDataGrid.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(uiDocumentDataGrid_CellToolTipTextNeeded);

并在事件处理程序

    void uiDocumentDataGrid_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
    {
        if (e.ColumnIndex == 0)
            e.ToolTipText = "Check this box to select this document.";
    }

答案 2 :(得分:0)

将此代码添加到CellFormatting事件中为我排序。我正在为后代添加这个答案,但是也会接受LarsTech的答案作为他的作品,而且他还是指出了这个事件。

private void uiDocumentDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if(e.ColumnIndex == 0)
        {
            DataGridViewCell cell = this.uiDocumentDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.ToolTipText = "Check this box to select this document.";
        }
    }