vb.net为datagridviewcombobox

时间:2016-01-28 02:15:17

标签: vb.net datagridview tooltip datagridviewcombobox

我已经搜索过显示datagridview的tooltiptext但是我只获得了datagridviewcell的tooltiptext。我想要的是从datagridviewcomboboxcolumn的下拉列表中突出显示项目(鼠标悬停)时显示的工具提示。 我在comboboxcolumn的数据绑定中设置了too​​ltiptext,但它在运行时没有显示任何内容。

'assuming dtExpense is the datatable used as datasource
With CType(DataGridView3.Columns(2), DataGridViewComboBoxColumn)
    .AutoComplete = True
    .DataSource = dtExpense
    .DisplayMember = "acct_title"
    .ValueMember = "acct_id"
    .DataPropertyName = "acct_id"
    .ToolTipText = "description"
End With

任何人都可以告诉我如何做到这一点。在datagriviewcell.tooltiptext中,它必须在某个时刻绘制。我正在考虑如何使用datagridviewcomboboxcolumn执行此操作,并且必须显示组合框中的每个项目。

1 个答案:

答案 0 :(得分:0)

假设您有一个名为string的{​​{1}}属性的对象类(显示为下拉项)和acct_title(要显示为这些下拉项目的工具提示),您就是&#39 ;我想:

  1. 将ToolTip控件添加到表单。
  2. 处理DataGridView的description事件,以将事件处理程序添加到基础ComboBox。

    EditingControlShowing
  3. 处理ComboBox Me.DataGridView3.EditingControlShowing += DataGridView3_EditingControlShowing Private Sub DataGridView3_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) If TypeOf e.Control Is ComboBox Then Dim cb As ComboBox = TryCast(e.Control, ComboBox) cb.DrawMode = DrawMode.OwnerDrawFixed cb.DrawItem -= Cb_DrawItem cb.DrawItem += Cb_DrawItem cb.DropDownClosed -= Cb_DropDownClosed cb.DropDownClosed += Cb_DropDownClosed End If End Sub 事件以设置ToolTip值并显示它。我们将使用反射从下拉项中获取DrawItem属性,并将其字符串值设置为工具提示。

    description
  4. 处理ComboBox Private Sub Cb_DrawItem(sender As Object, e As DrawItemEventArgs) Dim cb As ComboBox = TryCast(sender, ComboBox) Dim item = cb.Items(e.Index) Dim display As String = cb.GetItemText(item) Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString() e.DrawBackground() Using br As New SolidBrush(e.ForeColor) e.Graphics.DrawString(display, e.Font, br, e.Bounds) End Using If (e.State And DrawItemState.Focus) = DrawItemState.Focus AndAlso cb.DroppedDown Then Me.toolTip1.Show(toolText, cb, e.Bounds.Right, e.Bounds.Bottom, 2000) End If e.DrawFocusRectangle() End Sub 事件以隐藏工具提示。

    DropDownClosed